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;
}

 

posted @ 2014-08-14 19:35  Run_dream  阅读(151)  评论(0编辑  收藏  举报