B: Nearest Sequence
Description
Do you remember the “Nearest Numbers”? Now here comes its brother:”Nearest Sequence”.Given three sequences of char,tell me the length of the longest common subsequence of the three sequences.
Input
There are several test cases.For each test case,the first line gives you the first sequence,the second line gives you the second one and the third line gives you the third one.(the max length of each sequence is 100)
Output
For each test case,print only one integer :the length of the longest common subsequence of the three sequences.
Sample Input
abcd
abdc
dbca
abcd
cabd
tsc
Sample Output
2
1
Hint
三个字符串的LCS,直接套两个的那个来就好了
#include <bits/stdc++.h>
#define N 10100
#define INF 0x3f3f3f3f
#define LL long long
#define mem(a,n) memset(a,n,sizeof(a))
#define fread freopen("in.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)
using namespace std;
int dp[110][110][110];
char str0[110],str1[110],str2[110];
int main()
{
// ios::sync_with_stdio(false);
str0[0]=str1[0]=str2[0]='#';
while(~scanf("%s",str0+1)){
scanf("%s%s",str1+1,str2+1);
int len1=strlen(str0),len2=strlen(str1),len3=strlen(str2);
// cout<<len1<<len2<<len3<<endl;
int ans=0;
for(int i=1;i<len1;++i){
for(int j=1;j<len2;++j){
for(int k=1;k<len3;++k){
int a=max(dp[i-1][j][k],max(dp[i][j-1][k],dp[i][j][k-1]));
int b=max(dp[i-1][j-1][k],max(dp[i][j-1][k-1],dp[i-1][j][k-1]));
int c=max(a,b);
if(str0[i]==str1[j]&&str1[j]==str2[k]){
dp[i][j][k]=max(c,dp[i-1][j-1][k-1]+1);
}else{
dp[i][j][k]=c;
}
if(dp[i][j][k]>ans){
ans=dp[i][j][k];
}
}
}
}
printf("%d\n",ans);
}
return 0;
}
2907

被折叠的 条评论
为什么被折叠?



