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
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 there will be at least one common word in each text. Each text 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
3、用栈道搜索原来求得结果
#include<iostream> #include<cstdio> #include<string> #include<cstring> #include<cmath> #include<algorithm> #include<map> #include<stack> using namespace std; int t[1100][1100],a[1100],b[1100]; string s; int main() { while(1) { map<string,int>ma; map<int ,string>fma; int alen=1,blen=1,num=1,f=0; while(cin>>s&&s[0]!='#')//map转化 { if(ma[s]) a[alen]=ma[s]; else { a[alen]=num; ma[s]=num; fma[num]=s; num++; } alen++; f=1; } if(!f)break; while(cin>>s&&s[0]!='#') { if(ma[s]) b[blen]=ma[s]; else { b[blen]=num; ma[s]=num; fma[num]=s; num++; } blen++; } for(int i=1; i<alen; i++)//最长公共值序列 for(int j=1; j<blen; j++) { if(a[i]==b[j]) t[i][j]=t[i-1][j-1]+1; else t[i][j]=max(t[i-1][j],t[i][j-1]); } stack<int>st; int x=alen-1,y=blen-1; while(1)//寻找结果 { if(x==0||y==0)//找到位置 break; if(t[x][y]-1==t[x-1][y-1]&&a[x]==b[y])//寻找到加的位置 { //一定要加上a[x]==b[y]来确定 st.push(a[x]); x--,y--; } else if(t[x][y]==t[x-1][y])//向一边搜索 { x--; } else y--; } while(!st.empty())//打印结果 { int t=st.top(); st.pop(); if(!f) cout<<' '; cout<<fma[t]; f=0; } cout<<endl; } return 0; }