POJ 2250 (LCS longest common sub sequence)

Source: Internet
Author: User

CompromiseTime limit:1000MS Memory Limit:65536KB 64bit IO Format:%i64d &%i64 U

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

Exercises

Test instructions: At first glance, the input is a bit long, read carefully read, found that is to find a longest common sub-sequence and print.

Analysis: It is not difficult to find the longest common subsequence, and it is difficult to print one of its paths. We know that a two-dimensional array dp is used to save its number of matches. Once a match is made, it modifies the value behind it, guaranteeing

How a state is the largest value in the current DP data. In order to record its path. We need to use the array T to record. Can be implemented by recursive return.

It may be too abstract to say, attach a two-dimensional picture.

Abcbdab

Bdcaba

Two sequences, which are the longest common sub-sequences. The graph is the path backtracking

#include <iostream>#include<cstdio>#include<cstring>using namespacestd;intt[ the][ the],dp[ the][ the];stringa[ the],b[ the];voidLCsintXinty) {     for(intI=1; i<=x; i++)         for(intj=1; j<=y; J + +)        {            if(a[i]==B[j]) {Dp[i][j]=dp[i-1][j-1]+1; T[I][J]=1; }            Else            {                if(dp[i][j-1]<=dp[i-1][j]) {Dp[i][j]=dp[i-1][j]; T[I][J]=2; }                Else{Dp[i][j]=dp[i][j-1]; T[I][J]=3; }            }        }}voidOutputintXinty) {    if(x==0|| y==0)return; if(t[x][y]==1) {output (x-1, Y1); cout<<a[x]<<" "; }    Else if(t[x][y]==2) output (x-1, y); Else if(t[x][y]==3) output (x, y-1);}intMain () {strings;  while(cin>>s) {intm=2, n=1; a[1]=s;  while(cin>>s&&s!="#") {a[m++]=s; }         while(cin>>s&&s!="#") {B[n++]=s;        } LCS (M,n);        Output (m,n); cout<<Endl; }    return 0;}

POJ 2250 (LCS 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.