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.ios
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).promise
Your country needs this program, so your job is to write it for us.less
The input file will contain several test cases.ide
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 '#'.ui
Input is terminated by end of file.this
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.spa
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 #
die einkommen der abgeordneten muessen dringend verbessert werden
最長公共子序列問題,不過此次不僅要求出長度,還要寫出最長公共子序列。code
用兩個數組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。blog
求解完最長公共子序列以後,能夠從末尾一點一點找回去,當找到x=pos_a[i][j]和y=pos_b[i][j]時,若字符串a[x]==b[y],那麼就將這個字符串記錄下來,最後將全部記錄下來的字符串倒序打印出來便可
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 }