Fifth week of Camp D LCS

Source: Internet
Author: User

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

This problem is a variant of LCS, just change the array type to string, the focus is on the LCS template understanding
LCS: Solving the longest common subsequence, since the dynamic programming is nothing more than a recursive optimization, let's first look at the recursive solution
LCS (I,J) indicates that the array is the longest common sub-sequence up to the I of the array 1 and the first J of the array 2, noting that the I-character is the end character of the substring (in order to achieve the purpose of no-effect)
1.LCS (i-1,j-1) +1 (a "I" =b "J")
LCS (I,J) ={
2.max{lcs (I-1,j), LCS (I,j-1)} (a "I"!) =b "J")

The state transfer equation can be written by comparing recursion, and a two-dimensional array dp "I" "J" can be used to represent the state LCS (I,J)
However, this dynamic programming equation belongs to the unknown type from known push, to pay special attention to the initial value

1.DP "I-1" "J-1" +1 (a "I" =b "J") //If equal, the length of the common string is added one more
DP "I" "J" ={  2.dp "0" "I" =0,DP "I" "0" =0          //initial value, if the length of an array is 0, then there is no need to say the public string

3.MAX{DP "I-1" "J", DP "I" "J-1"} (a "I"!) =b "J") //If not equal, only the state with the largest previous value can be selected


Then, because of the problem system, when DP seeks the longest common substring, record the selected characters, the final output is good


#include"Iostream"#include"CString"UsingNamespace Std;StringA[110];StringB[110];StringC[110];int DP[110][110];int D[110][110];IntMain(){int L1-ls;While(CIN>>a[1]){L1=2;While(A[L1-1]!="#") CIN>>a[L1++]; L1-=2; Cin>>b[1]; L2=2;While(b[L2-1]!="#") CIN>>b[L2++];Memset(DP,0,sizeof(DP));Memset(d,0,sizeof(d));For(int I=1; I<=l1; I++)For(Int J=1; j<=l2; j++){If(A[I]==b[j]){DP[I][j]=dp[I-1][j-1]+1; D[I][j]=3;}Else{If(DP[I-1][j]>dp[I][j-1]){DP[I][j]=dp[I-1][j]; D[I][j]=2;}Else{DP[I][j]=dp[I][j-1]; D[I][j]=1;}}}int n=dp[L1][l2];int I=l1;Int J=l2;While(n){While(d[I][j]!=3){If(d[I][j]==2) I--;Else J--;} n--; C[n]=a[I--;j--;  Cout<<c[ 0); for (int k=1;k<dp[l1][l2];k++) cout< < "<<c[k <<endl; }}          /span>                

Fifth week of Camp D LCS

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.