CSU 1060

1060: Nearest Sequence

Time Limit: 1 Sec  Memory Limit: 64 MB
Submit: 370  Solved: 118
[Submit][Status][Web Board]

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

 

Source

CSU Monthly 2012 May.

 

题目没有读懂

这个题目的意思和最长上升子序列是一样的

不要求连续

看了一下AC代码

用dp写的

意思也很明白

#include <iostream>
#include <string>
#include <cstring>
using namespace std;
const int maxn = 105;
char s1[maxn],s2[maxn],s3[maxn];
int dp[maxn][maxn][maxn];

int main(){
    string s1,s2,s3;
    while(cin>>s1){
        cin>>s2>>s3;
        memset(dp,0,sizeof(dp));
        //三重循环
        for(int i = 1;i <= s1.length();i++)
        {
            for(int j = 1;j <= s2.length();j++)
            {
                for(int k = 1;k <= s3.length();k++)
                {
                    if(s1.at(i-1) == s2.at(j-1) && s2.at(j-1) == s3.at(k-1))
                    {
                        dp[i][j][k] = dp[i-1][j-1][k-1]+1;//三个位置相同
                    }
                    else
                    {
                        int tmp1 = dp[i-1][j][k];
                        int tmp2 = dp[i][j-1][k];
                        int tmp3 = dp[i][j][k-1];
                        int ans = max(tmp1,tmp2);//不同则为之前保存的最大值
                        ans = max(ans,tmp3);
                        dp[i][j][k] = ans;
                    }
                }
            }
        }

        cout<<dp[s1.length()][s2.length()][s3.length()]<<endl;;
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/Run-dream/p/3913294.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值