POJ 2250 Compromise (linear DP LCS + recursive path)

Source: Internet
Author: User

Compromise
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 6735 Accepted: 3009 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

Source

ulm Local 1997

Topic Link: http://poj.org/problem?id=2250

The main idea: to find the maximum common sub-sequence of the words in two paragraphs and output them

Topic Analysis: Bare LCS problem, because it is a word, with a string on the line, two-dimensional character array can also, and then use an array to record the path
The state-state transition equation for the longest common subsequence of LCS is explained by:
Dp[i][j] Represents the length of the longest common subsequence that represents the first I-bit and B's former J-bits of a, and then there are three cases
1. If a[i-1] = = B[j-1],dp[i][j] = dp[ I-1][J-1] + 1 that is, if a[i-1] = = B[j-1], then this group of identical cases is recorded as +1, then we just continue to compare the former i-1 of A and B of the former j-1 part of
2. If DP[I-1][J] > Dp[i][j-1],d P[I][J] = Dp[i-1][j] That is, the former I-1 bits of a and the LCS of the former J-bit of B are greater than a the former I bits and B of the former j-1 bit , then we take the large
3. if DP[I-1][J] < dp[i][j-1],dp[i][j] = Dp[i][j-1]
The subject needs to record the path, we use the path array, if the case is 1 path[i][j] = 0, if the case 2 Path[i][j] = 1, otherwise path[i][j] = 1, then recursively records the answer

#include <iostream> #include <cstring> #include <string> #include <algorithm>using namespace  Std;int dp[105][105];int path[105][105];string ans[105];string a[105], B[105];int Cnta, CNTB, CNT, num;void init () {CNT    = Cnta = CNTB = num = 0;    memset (DP, 0, sizeof (DP)); memset (path, 0, sizeof (path));    void Get (int i, int j) {if (!i | |!j) return;        if (path[i][j] = = 0) {Get (i-1, j-1);    Ans[num + +] = a[i-1];    } else if (path[i][j] = = 1) Get (I-1, J); else Get (i, j-1);}    int main () {string S;    Init ();        while (Cin >> s) {if (s[0] = = ' # ') cnt + +;        if (cnt = = 0 && s[0]! = ' # ') A[cnta + +] = s;        if (cnt = = 1 && s[0]! = ' # ') B[cntb + +] = s; if (cnt = = 2) {for (int i = 1; I <= Cnta; i++) {for (int j = 1; J <= Cntb ;     J + +) {if (a[i-1] = = B[j-1])               {Dp[i][j] = dp[i-1][j-1] + 1;                    PATH[I][J] = 0;  } else if (Dp[i-1][j] > Dp[i][j-1]) {Dp[i][j] = dp[i-                        1][J];                    PATH[I][J] = 1;                        } else {dp[i][j] = dp[i][j-1];                    PATH[I][J] =-1;            }}} Get (Cnta, CNTB);            for (int i = 0; i < num-1; i++) cout << ans[i] << "";            cout << ans[num-1] << Endl;        Init (); }    }}




POJ 2250 Compromise (linear DP LCS + recursive path)

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.