UVA 531 · Compromise (LCS---path printing)

Source: Internet
Author: User

Topic:

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 Specification

The input file 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 Specification

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
Resolution: Simple LCS but added path to print, and 01 backpack path printing is different
The input method of this problem is worth learning, enter a large string of strings until the character "#" appears.
You can use a two-dimensional array, first enter the first word, to determine whether the first letter is "#".
1#include <cstdio>2#include <cstring>3 Const intmaxn= the;4 Charstr1[maxn][ +],str2[maxn][ +];5 intDp[maxn][maxn],path[maxn][maxn],flag;6 voidPrintintXinty)7 {8     if(!x| |!y)9         return;Ten     if(path[x][y]==1) One     { APrint (X-1, Y1); -         if(flag) -printf" "); the         Else -flag=1; -printf"%s", str1[x]); -     } +     Else if(path[x][y]==0) -Print (X-1, y); +     Else APrint (x,y-1); at } - intMain () - { -     intnum1,num2; -      while(SCANF ("%s", str1[1])==1) -     { innum1=1; -Num2=0; to         if(str1[1][0]!='#') +         { -              for(intI=2;; i++) the             { *scanf"%s", Str1[i]); $                 if(str1[i][0]=='#') Break;Panax Notoginsengnum1++; -             } the         } +          for(intI=1;; i++) A         { thescanf"%s", Str2[i]); +             if(str2[i][0]=='#') Break; -num2++; $         } $Memset (DP,0,sizeof(DP)); -          for(intI=1; i<=num1;i++) -         { the              for(intj=1; j<=num2;j++) -             {Wuyi                 if(!strcmp (Str1[i],str2[j])) the                 { -dp[i][j]=dp[i-1][j-1]+1; Wupath[i][j]=1; -                 } About                 Else if(dp[i-1][j]>dp[i][j-1]) $                 { -dp[i][j]=dp[i-1][j]; -path[i][j]=0; -                 } A                 Else +                 { thedp[i][j]=dp[i][j-1]; -path[i][j]=-1; $                 } the             } the         } theflag=0; the print (num1,num2); -printf"\ n"); in     } the     return 0; the}
View Code

UVA 531 · Compromise (LCS---path printing)

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.