XiphoRAY


私信TA

用户名:a2473108024

访问量:3245

签 名:

Remilitarize on the way of ACM

等  级
排  名 3660
经  验 1796
参赛次数 3
文章发表 1
年  龄 19
在职情况 学生
学  校 大连交通大学
专  业 机械工程+软件工程

  自我简介:

TA的其他文章

解体思路:


贪心

对字符串 A 的增删改都会导致操作次数 + 1,所以只需找出字符串 B 中能够对应到字符串 A 中的字符数目(即不需改动的字符),用 A.Length() - count 即可求得。

注意事项:

注意到有可能字符串 B 的第一个字符对应到字符串 A 偏后的字符,所以需要循环 B.Length() 次,忽略掉字符串 B 中的各个字符,依次求解,取最小值。

参考代码:

#include<iostream>
#include<string>
#include<cstring>
using namespace std;

const int maxn = 200 + 5;

int main(){

    string a, b;
    cin >> a >> b;

    int ans = maxn;

    for(int i = 0; i < b.length(); i ++){
        int count = 0;

        int posa, posb, pos = 0;
   	    for(posb = i; posb < b.length(); posb ++){
            for(posa = pos; posa < a.length(); posa ++){
                if(a[posa] == b[posb]){
                    pos = posa + 1;
                    count ++;
                }
            }
        }
        ans = ans > (a.length() - count) ? (a.length() - count):ans;
    }

    cout << ans << endl;

    return 0;
}


 

0.0分

22 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答

代码解释器

  评论区

这写的有问题
2020-10-15 20:07:29
代码答案是错的,比如1234和4321输入理论输出为4,你的输出为3
2020-02-14 18:48:02
  • «
  • 1
  • »