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 <iostream> # include <cstdio> # include <cstring> # include <algorithm> # include <limits. h> 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 <num; I ++) printf ("% s ", s [I]); printf ("\ n");} return 0 ;}