Compromise |
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.
Input Specification
The input file 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.
Output Specification
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
最长公共子序列问题,不过这次不只要求出长度,还要写出最长公共子序列。
用两个数组pos_a[i][j]和pos_b[i][j]记录每次转移的动向,比如dp[i][j]是由dp[i-1][j]转移过来的,那么pos_a[i][j]=i-1,pos_b[i][j]=j。
求解完最长公共子序列之后,可以从末尾一点一点找回去,当找到x=pos_a[i][j]和y=pos_b[i][j]时,若字符串a[x]==b[y],那么就将这个字符串记录下来,最后将所有记录下来的字符串倒序打印出来即可
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #include<iostream> 2 #include<string> 3 #include<cstring> 4 #include<stack> 5 #include<cstdio> 6 7 using namespace std; 8 9 string a[105],b[105]; 10 int dp[105][105],pos_a[105][105],pos_b[105][105]; 11 12 int main() 13 { 14 while(cin>>a[1]) 15 { 16 int p,q; 17 for(p=1;a[p]!="#";p++) 18 cin>>a[p+1]; 19 cin>>b[1]; 20 for(q=1;b[q]!="#";q++) 21 cin>>b[q+1]; 22 23 memset(dp,0,sizeof(dp)); 24 memset(pos_a,-1,sizeof(pos_a)); 25 memset(pos_b,-1,sizeof(pos_b)); 26 27 for(int i=1;i<p;i++) 28 for(int j=1;j<q;j++) 29 { 30 31 if(a[i]==b[j]) 32 { 33 dp[i][j]=dp[i-1][j-1]+1; 34 pos_a[i][j]=i-1; 35 pos_b[i][j]=j-1; 36 } 37 else 38 { 39 if(dp[i-1][j]>dp[i][j-1]) 40 { 41 dp[i][j]=dp[i-1][j]; 42 pos_a[i][j]=i-1; 43 pos_b[i][j]=j; 44 } 45 else 46 { 47 dp[i][j]=dp[i][j-1]; 48 pos_a[i][j]=i; 49 pos_b[i][j]=j-1; 50 } 51 } 52 } 53 int x=p-1; 54 int y=q-1; 55 56 stack<string> s; 57 58 while(x>0&&y>0) 59 { 60 if(a[x]==b[y]) 61 s.push(a[x]); 62 int temp=x; 63 x=pos_a[x][y]; 64 y=pos_b[temp][y]; 65 } 66 67 while(s.size()>1) 68 { 69 cout<<s.top()<<' '; 70 s.pop(); 71 } 72 cout<<s.top()<<endl; 73 } 74 75 return 0; 76 }