csuoj1060: 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

用dp开个三维数组,如果难以理解的话可以先做poj1458Common Subsequence这道题,它只要比较两个字符串  http://poj.org/problem?id=1458
我的这个代码还可以优化,不过A了就没管了,三个循环一个一个比过去就OK的。
AC代码:
#include<iostream>
#include<cstring>
using namespace std;
char s1[105],s2[105],s3[105];
int dp[105][105][105];
int ans;
void DP()
{
  memset(dp,0,sizeof dp);
  int n1=strlen(s1);
  int n2=strlen(s2);
  int n3=strlen(s3);
  for(int i=1;i<=n1;i++)
  for(int j=1;j<=n2;j++)
  for(int k=1;k<=n3;k++)
  {
    if(s1[i-1]==s2[j-1]&&s2[j-1]==s3[k-1])
    dp[i][j][k]=dp[i-1][j-1][k-1]+1;
    else
    dp[i][j][k]=max(dp[i-1][j][k],max(dp[i][j-1][k],dp[i][j][k-1]));
  }
  ans=dp[n1][n2][n3];
}
int main()
{
  while(cin>>s1>>s2>>s3)
  {
    DP();
    cout<<ans<<endl;
  }
  return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值