题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87125#problem/D
题目:
Description
In a few months the European Currency Union will become a reality. However, to join the club, the Maastricht criteria must be fulfilled, and this is not a trivial task for the countries (maybe except for Luxembourg). To enforce that Germany will fulfill the criteria, our government has so many wonderful options (raise taxes, sell stocks, revalue the gold reserves,...) that it is really hard to choose what to do.
Therefore the German government requires a program for the following task:
Two politicians each enter their proposal of what to do. The computer then outputs the longest common subsequence of words that occurs in both proposals. As you can see, this is a totally fair compromise (after all, a common sequence of words is something what both people have in mind).
Your country needs this program, so your job is to write it for us.
Therefore the German government requires a program for the following task:
Two politicians each enter their proposal of what to do. The computer then outputs the longest common subsequence of words that occurs in both proposals. As you can see, this is a totally fair compromise (after all, a common sequence of words is something what both people have in mind).
Your country needs this program, so your job is to write it for us.
Input
The input will contain several test cases.
Each test case consists of two texts. Each text is given as a sequence of lower-case words, separated by whitespace, but with no punctuation. Words will be less than 30 characters long. Both texts will contain less than 100 words and will be terminated by a line containing a single '#'.
Input is terminated by end of file.
Each test case consists of two texts. Each text is given as a sequence of lower-case words, separated by whitespace, but with no punctuation. Words will be less than 30 characters long. Both texts will contain less than 100 words and will be terminated by a line containing a single '#'.
Input is terminated by end of file.
Output
For each test case, print the longest common subsequence of words occuring in the two texts. If there is more than one such sequence, any one is acceptable. Separate the words by one blank. After the last word, output a newline character.
Sample Input
die einkommen der landwirte
sind fuer die abgeordneten ein buch mit sieben siegeln
um dem abzuhelfen
muessen dringend alle subventionsgesetze verbessert werden
#
die steuern auf vermoegen und einkommen
sollten nach meinung der abgeordneten
nachdruecklich erhoben werden
dazu muessen die kontrollbefugnisse der finanzbehoerden
dringend verbessert werden
#
Sample Output
die einkommen der abgeordneten muessen dringend verbessert werden
题意:
给出两段单词,求出长度最大的公共子序列(单词组)。
分析:
设dp[i][j]为a[i]和b[j]的LCS长度,当a[i]=b[j]时,dp[i][j]=dp[i-1][j-1]+1;
否则dp[i][j]=max{dp[i-1][j],dp[i][j-1]}.
标记输出(就是要将每次的决策给记录下来,方便输出的时候用,每次输出的时候是当他们相等的时候决策就输出。)。
1 #include<iostream> 2 #include<string> 3 using namespace std; 4 string a[105]; 5 string b[105]; 6 string c[105]; 7 int dp[105][105],d[105][105]; 8 int main() 9 { 10 int n,m,i,j; 11 while(cin>>a[1]) 12 { 13 n=2; 14 while(a[n-1]!="#") 15 cin>>a[n++]; 16 n-=2; 17 cin>>b[1]; 18 m=2; 19 while(b[m-1]!="#") 20 cin>>b[m++]; 21 memset(dp,0,sizeof(dp)); 22 memset(d,0,sizeof(d)); 23 for(i=1;i<=n;i++) 24 { 25 for(j=1;j<=m;j++) 26 { 27 if(a[i]==b[j]) 28 { dp[i][j]=dp[i-1][j-1]+1; 29 d[i][j]=3;} //标记 30 else 31 if(dp[i-1][j]>dp[i][j-1]) 32 { dp[i][j]=dp[i-1][j];d[i][j]=1;} 33 else {dp[i][j]=dp[i][j-1];d[i][j]=2;} 34 } 35 } 36 int L=dp[n][m]; 37 i=n; 38 j=m; 39 while(L) 40 { 41 while(d[i][j]!=3) 42 { 43 if(d[i][j]==1) 44 i--; 45 else j--; 46 } 47 L--; 48 c[L]=a[i]; 49 i--;j--; 50 } 51 cout<<c[0]; 52 for(int k=1;k<dp[n][m];k++) 53 cout<<" "<<c[k]; 54 cout<<endl; 55 } 56 return 0; 57 }