解题思路:                    字符A修改为字符B的次数至少是字符A长度减去字符B的长度(B长就B减去A)

                                    例如输入:


                                                  A:  sfdxbqw      
                                                  B:  gfdgw
                                    那么A修改为字符B的删除或者插入次数至少是sfdxbqw-gfdgw(7-5),至少得2次,

                                    由于字符A修改为字符B肯定有字符需要修改A字符的其他元素,

                                    所以字符A修改的次数肯定是根据AB的不同进行修改的,

                                    那意味着每一次的不同都需要进行一次修改,

                                    现在我们可以考虑字符比较法,对A和B进行比较,如果不相同,计数器就增加

                                  

                                            
注意事项:

参考代码:

        

                                        #include<iostream>
                                        using namespace std;
                                        int main()
                                        {
                                                        string num,nums;
                                                        int a,b,c;
                                                        cin>>num>>nums;
                                                        b=num.size();
                                                        c=nums.size();
                                                        if(b>c)                                                        //比较输入字符长度
                                                       {
                                                                a=b-c;
                                                                for(int i=0;i<c-1;i++)
                                                                {
                                                                    if(num[i]!=nums[i])
                                                                        a++;
                                                                }
                                                      }
                                                      else
                                                      {
                                                               a=c-b;
                                                               for(int i=0;i<b-1;i++)
                                                                       if(num[i]!=nums[i])
                                                                               a++;
                                                       }
                                                      cout<<a;
                                                      return 0;
                                             }

点赞(0)
 

0.0分

3 人评分

C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:

一点编程也不会写的:零基础C语言学练课程

解决困扰你多年的C语言疑难杂症特性的C语言进阶课程

从零到写出一个爬虫的Python编程课程

只会语法写不出代码?手把手带你写100个编程真题的编程百练课程

信息学奥赛或C++选手的 必学C++课程

蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程

手把手讲解近五年真题的蓝桥杯辅导课程

评论列表 共有 4 条评论

啊北 2年前 回复TA
为啥c-1和b-1啊
whnd1831 3年前 回复TA
题解有问题把,
sfdxbgw
gfdgw
输出仍是4,题目给的数据有毛病
wuhu 3年前 回复TA
想问一下如果输入ab,想变成ba,为什么程序跑出来是1呢
hwq 4年前 回复TA
感谢答主分享,既然是每一次都要比较,为什么循环要-1呢?