Compromise
Time Limit: 1000 MS Memory Limit: 65536 K
Total Submissions: 4965 Accepted: 2266 Special Judge
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 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
Start learning about DP ). This question requires the longest common subword "word". It is annoying to input ....... then the longest public substring template is applied ....... print is to use the flag to start from the end point and print recursively (similar to the print path in graph theory ).
[Cpp]
# Include <iostream>
# Include <algorithm>
# Include <cmath>
# Include <cstdio>
# Include <cstdlib>
# Include <cstring>
# Include <string>
# Include <vector>
# Include <set>
# Include <queue>
# Include <stack>
# Include <climits> // values such as INT_MAX
# Deprecision MAX 1050
# Define INF 0x7FFFFFFF
# Define eps 1e-5
Using namespace std;
Char a [101] [31], B [101] [31];
Int len [1, 6005] [2, 6005];
Int index [6005] [6005]; // flag
Void print (int I, int j) // template Printing
{
If (I> = 1 & j> = 1)
{
If (index [I] [j] = 0)
{
Print (I-1, J-1 );
If (j! = 1)
Cout <'';
Cout <B [J-1];
}
If (index [I] [j] = 1)
{
Print (I, J-1 );
}
If (index [I] [j] =-1)
{
Printing (I-1, j );
}
}
}
Int main ()
{
Int I, j;
Char d [50];
While (cin> d)
{
Memset (a, '\ 0', sizeof ());
Memset (B, '\ 0', sizeof (B ));
I = 0;
While (strcmp (d ,"#")! = 0) // The first dictionary Input
{
Strcpy (a [I], d );
I ++;
Scanf ("% s", d );
}
Scanf ("% s", d );
J = 0;
While (strcmp (d ,"#")! = 0) // second dictionary Input
{
Strcpy (B [j], d );
J ++;
Scanf ("% s", d );
}
Int len1 = I;
Int len2 = j;
For (I = 0; I <len1; I ++)
{
Len [I] [0] = 0;
}
For (I = 0; I <len2; I ++)
{
Len [0] [I] = 0;
}
For (I = 1; I <= len1; I ++) // The Longest Common substring of the template.
For (j = 1; j <= len2; j ++)
{
If (strcmp (a [I-1], B [J-1]) = 0)
{
Len [I] [j] = len [I-1] [J-1] + 1;
Index [I] [j] = 0;
}
Else
{
If (len [I-1] [j]> len [I] [J-1])
{
Len [I] [j] = len [I-1] [j];
Index [I] [j] =-1;
}
Else
{
Len [I] [j] = len [I] [J-1];
Index [I] [j] = 1;
}
}
}
Print (len1, len2); // print recursively from the end point
Cout <endl;
Cout <endl;
// Cout <len [len1] [len2] <endl;
}
Return 0;
}