Weekly POJ 2250 Compromise
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 failed t for Luxembourg ). to enforce that Germany will fulfill the criteria, our government has so far wonderful options (raise taxes, destination 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 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
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 landwirtesind fuer die abgeordneten ein buch mit sieben siegelnum dem abzuhelfenmuessen dringend alle subventionsgesetze verbessert werden#die steuern auf vermoegen und einkommensollten nach meinung der abgeordnetennachdruecklich erhoben werdendazu muessen die kontrollbefugnisse der finanzbehoerdendringend verbessert werden#
Sample Output
die einkommen der abgeordneten muessen dringend verbessert werden
The word LCS should be recursively output =-=. I was so stupid that I wanted to simulate it during the competition.
# Include
# Include
# Include
# Include
Using namespace std; char s1 [110] [110], s2 [110] [110]; char s [110] [110]; int mark [110] [110], dp [110] [110]; int len1, len2, num; void LCS () {memset (dp, 0, sizeof (dp); memset (mark, 0, sizeof (mark); for (int I = 0; I <= len1; I ++) mark [I] [0] = 1; for (int I = 0; I <= len2; I ++) mark [0] [I] =-1; for (int I = 1; I <= len1; I ++) {for (int j = 1; j <= len2; j ++) {if (! Strcmp (s1 [I-1], s2 [J-1]) {dp [I] [j] = dp [I-1] [J-1] + 1; mark [I] [j] = 0; // 0 indicates s1 [I-1] = s2 [J-1] for Recursive output} else if (dp [I-1] [j]> = dp [I] [J-1]) {dp [I] [j] = dp [I-1] [j]; mark [I] [j] = 1; // likewise} else {dp [I] [j] = dp [I] [J-1]; mark [I] [j] =-1 ;}}}} void print (int I, int j) {if (! I &&! J) return; if (mark [I] [j] = 0) {print (I-1, J-1); strcpy (s [num ++], s1 [I-1]);} else if (mark [I] [j] = 1) print (I-1, j); else print (I, J-1 );} int main () {while (~ Scanf ("% s", s1 [0]) {len1 = 1; while (strcmp (s1 [len1-1], "#") scanf ("% s ", s1 [len1 ++]); scanf ("% s", s2 [0]); len2 = 1; while (strcmp (s2 [len2-1], "#") scanf ("% s", s2 [len2 ++]); len1 --; len2 --; num = 0; LCS (); print (len1, len2 ); printf ("% s", s [0]); for (int I = 1; I