题意:

构造一个字符串 $c$,使 $a$ 为 $c$ 的字串且 $b$ 为 $c$ 的子序列,求 $c$ 的最小长度。

思路:

设 $a$ 的长度为 $lena$,$b$ 的长度为 $lenb$,$a$ 与 $b$ 的最大公共子序列长度为 $s$,因为 $c$ 中一定包含 $a$ 跟 $b$ 中的所有字符,所以我们可以知道 $c$ 的最小长度为 $lena+lenb-s$。

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include<bits/stdc++.h>
using namespace std;
int t,maxn=-1;
string s,ss;
int main(){
cin>>t;
while(t--){
cin>>s>>ss;
int lena,lenb;
lena=s.size();
lenb=ss.size();
maxn=-1;
for(int i=0;i<lenb;i++){
int n=i,len=0;
for(int j=0;j<lena;j++){
if(s[j]==ss[n]){
n++;
len++;
}
}
maxn=max(maxn,len);
}
cout<<lena+lenb-maxn<<endl;
}
return 0;
}