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.
标签:des style blog http color os io for
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.
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)
For each test case,print only one integer :the length of the longest common subsequence of the three sequences.
abcd
abdc
dbca
abcd
cabd
tsc
2
1
题目没有读懂
这个题目的意思和最长上升子序列是一样的
不要求连续
看了一下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; }
标签:des style blog http color os io for
原文地址:http://www.cnblogs.com/Run-dream/p/3913294.html