CSU1060-Nearest Sequence-DP/LCS

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值