Compromise (POJ - 2250)

题面

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.

输入

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.

输出

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.

样例输入

 1die einkommen der landwirte
 2sind fuer die abgeordneten ein buch mit sieben siegeln
 3um dem abzuhelfen
 4muessen dringend alle subventionsgesetze verbessert werden
 5#
 6die steuern auf vermoegen und einkommen
 7sollten nach meinung der abgeordneten
 8nachdruecklich erhoben werden
 9dazu muessen die kontrollbefugnisse der finanzbehoerden
10dringend verbessert werden
11#

样例输出

1die einkommen der abgeordneten muessen dringend verbessert werden

提示

思路

代码

 1using namespace std;
 2typedef long long ll;
 3const int inf = 0x3f3f3f3f;
 4const int N = 1e2+5;
 5
 6string a[N], b[N], s;
 7int dp[N][N];
 8
 9void print(int i, int j) {
10    stack<string>st;
11    while(i&&j) {
12        if(a[i-1]==b[j-1]) {
13            st.push(a[i-1]);
14            i--, j--;
15        } else {
16            if(dp[i-1][j]>dp[i][j-1])
17                i--;
18            else
19                j--;
20        }
21    }
22    while(st.size()) {
23        cout<<st.top()<<" ";
24        st.pop();
25    }
26    cout<<endl;
27}
28
29int main(void) {
30    while(cin>>s) {
31        int n = 0, m = 0;
32        while(s!="#") {
33            a[n++] = s;
34            cin>>s;
35        }
36        cin>>s;
37        while(s!="#") {
38            b[m++] = s;
39            cin>>s;
40        }
41        memset(dp, 0, sizeof(dp));
42        for(int i=1; i<=n; i++) {
43            for(int j=1; j<=m; j++) {
44                if(a[i-1]==b[j-1])
45                    dp[i][j] = dp[i-1][j-1]+1;
46                else
47                    dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
48            }
49        }
50        print(n, m);
51    }
52    return 0;
53}