Double-ended Strings | Codeforces

Double-ended Strings


from Codeforces Round #710 (Div. 3)
Time limit:2s
Memory limit:256MB

在这里插入图片描述


暴力解决就好,这个题目的意思就是查找最大相同子串,我们可以选择的子串长度为(1)到(ab长度最小值),然后在a和b中选择不同的子串,判断能否找到该长度的相同子串。最后把ab都剔除为该长度的子串就ok了

ac代码:
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
string a,b;         //如题中的字串ab
int t,la,lb,maxn;   //t组测试,ab的长度,ab最大相同字串长度
int main(){
    cin>>t;
    while(t--){
        cin>>a>>b;
        maxn = 0;
        la = a.length(),lb = b.length();
        for(int l = 1;l <= la && l <= lb;++l){      //字串长度
            for(int i = 0;i < la - l + 1;++i){      //从a的哪个位置开始取子串
                for(int j = 0;j < lb - l + 1;++j){  //从b的哪个位置开始取子串
                    bool flag = true;
                    for(int notea = i,noteb = j;notea <= i + l - 1;++notea,++noteb)
                        if(a[notea] != b[noteb]){
                            flag = false;break;
                        }
                    if(flag)            //如果这对子串是相等的,那么更新相同子串最长长度
                        maxn = max(maxn,l);
                }
            }
        }
        cout<<la + lb - maxn * 2<<"\n";
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Alan_Lowe

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值