CSU1060-Nearest Sequence-DP/LCS

来源:互联网 发布:针织短款开衫淘宝网 编辑:程序博客网 时间:2024/06/14 06:05

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

abcdabdcdbcaabcdcabdtsc

Sample Output

21

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;}
原创粉丝点击