我有个喵喵酱


私信TA

用户名:firstblond

访问量:8646

签 名:

每天都要进步一点点!

等  级
排  名 705
经  验 3910
参赛次数 0
文章发表 19
年  龄 21
在职情况 学生
学  校 家里蹲
专  业

  自我简介:

解题思路:                    字符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分

4 人评分

  评论区

为啥c-1和b-1啊
2022-04-04 11:39:04
题解有问题把,
sfdxbgw
gfdgw
输出仍是4,题目给的数据有毛病
2021-02-21 19:42:18
想问一下如果输入ab,想变成ba,为什么程序跑出来是1呢
2021-02-01 10:30:42
感谢答主分享,既然是每一次都要比较,为什么循环要-1呢?
2020-08-26 00:54:31
  • «
  • 1
  • »