POJ 2250 Compromise (longest common sub-sequence)

Source: Internet
Author: User

Compromise
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 6750 Accepted: 3018 Special Judge

Description

In a few months the European Currency Union would become a reality. However, to join the club, the Maastricht criteria must being fulfilled, and this is not a trivial task for the countries (MA Ybe except for Luxembourg). To enforce that Germany would fulfill the criteria, our government have so many wonderful options (raise taxes, sell stocks, Revalue the Gold reserves,...) That's it's really hard to choose.

Therefore The German government requires a program for the following task:
Both politicians each enter their proposal of "what". The computer then outputs the longest common subsequence of words, the 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 hav E in mind).

Your Country needs This program, so Your job was to write it for us.

Input

The input would contain several test cases.
Each test case consists of texts. Each text was given as a sequence of lower-case words, separated by whitespace, and with no punctuation. Words'll is less than and characters long. Both texts would contain less than words and would 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. If there is more than one such sequence, all 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 su Bventionsgesetze verbessert werden#die Steuern auf Vermoegen und einkommensollten nach Meinung der Abgeordnetennachdrueck Lich Erhoben Werdendazu muessen die Kontrollbefugnisse der finanzbehoerdendringend Verbessert werden#

Sample Output

Die Einkommen der Abgeordneten Muessen dringend verbessert werden





Idea: Classic DP problem, I mapped a string array into an int array using a map, and then recursively printing the solution


#include <cstdio> #include <iostream> #include <cstring> #include <algorithm>using namespace STD; #include <map>map<string,int> all;char s[50];int s1[110],s2[110];int dp[110][110];string ache[220]; int DP (int x,int y) {if (x<0| |    Y&LT;0) return 0;    int &ans=dp[x][y];    if (dp[x][y]>=0) return ans;    if (S1[x]==s2[y]) ANS=DP (x-1,y-1) +1;    else return Ans=max (DP (X-1,Y), DP (X,Y-1)); return ans;} void print (int x,int y) {if (x<0| |    Y&LT;0) return;        if (S1[x]==s2[y]) {print (x-1,y-1);        if (DP (x, y) ==1) cout<<ache[s1[x]];    else cout<< ' <<ache[s1[x]];        } else {if (DP (x,y-1) &GT;DP (x-1,y)) print (x,y-1);    else print (x-1,y);    }}void Ini () {memset (dp,-1,sizeof (DP)); All.clear ();}        int main () {while (~SCANF ("%s", s)) {ini ();        if (s[0]== ' # ') continue;        int top=0;        int l1=0,l2=0;        String t=s;  if (All.count (t) ==0) all[t]=++top,ache[top]=t;      S1[L1++]=ALL[T];            while (scanf ("%s", s)) {if (s[0]== ' # ') break;            T=s;            if (All.count (t) ==0) all[t]=++top,ache[top]=t;        S1[L1++]=ALL[T];            } while (scanf ("%s", s)) {if (s[0]== ' # ') break;            T=s;            if (All.count (t) ==0) all[t]=++top,ache[top]=t;        S2[L2++]=ALL[T];        } dp[0][0]= (S1[0]==s2[0]);        DP (l1-1,l2-1);        Print (l1-1,l2-1);    Puts (""); } return 0;}


POJ 2250 Compromise (longest common sub-sequence)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.