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;
}