Compromise(求解最长公共子序列并输出)

Compromise
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 5181   Accepted: 2361   Special Judge

Description

In a few months the European Currency Union will become a reality. However, to join the club, the Maastricht criteria must be fulfilled, and this is not a trivial task for the countries (maybe except for Luxembourg). To enforce that Germany will fulfill the criteria, our government has so many wonderful options (raise taxes, sell stocks, revalue the gold reserves,...) that it is really hard to choose what to do.

Therefore the German government requires a program for the following task:
Two politicians each enter their proposal of what to do. The computer then outputs the longest common subsequence of words that 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 have in mind).

Your country needs this program, so your job is to write it for us.

Input

The input will contain several test cases.
Each test case consists of two texts. Each text is given as a sequence of lower-case words, separated by whitespace, but with no punctuation. Words will be less than 30 characters long. Both texts will contain less than 100 words and will 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 two texts. If there is more than one such sequence, any one is acceptable. Separate the words by one blank. After the last word, output a newline character.

Sample Input

die einkommen der landwirte
sind fuer die abgeordneten ein buch mit sieben siegeln
um dem abzuhelfen
muessen dringend alle subventionsgesetze verbessert werden
#
die steuern auf vermoegen und einkommen
sollten nach meinung der abgeordneten
nachdruecklich erhoben werden
dazu muessen die kontrollbefugnisse der finanzbehoerden
dringend verbessert werden
#

Sample Output

die einkommen der abgeordneten muessen dringend verbessert werden

Source

 
这是典型的求解最长公共子序列(lcs)的问题,不过这里的“字母”是单词而已。
设要求t1和t2两个字符串的lcs,dp[i][j]代表t1前i个字符和t2前j个字符的最长公共子序列的长度,则有
if i == 0 || j == 0  dp[i][j]=dp[i][j]=0;
else if t1[i] == t2[j]  dp[i][j]=dp[i-1][j-1]+1;
else dp[i][j]=max(dp[i][j-1], dp[i-1][j])
在求解dp的过程中自然能得到哪些字符是在lcs上,并能用path数组标记之,具体算法参见程序。
 
AC Code:
 1 #include <iostream>
 2 #include <string>
 3 #include <cstdio>
 4 
 5 using namespace std;
 6 
 7 string t1[105], t2[105], lcs[105]; //text1, text2, lcs
 8 int len1, len2, dp[105][105], path[105][105]; //path用于标记路径以便之后输出 
 9 
10 int main()
11 {
12      string t1[105], t2[105], s;
13      while(cin >> t1[1] && t1[1] != "#")  
14      {
15          /*为方便下标应从1开始,因为dp[i][j]的含义是模式串的前i个字符和
16          匹配串的前j个字符的lcs的长度。 */
17          //输入: 
18          for(len1 = 2; cin >> t1[len1] && t1[len1] != "#"; len1++){}
19          for(len2 = 1; cin >> t2[len2] && t2[len2] != "#"; len2++){}
20          
21          //求lcs并标记路径 : 
22          for(int i = 1; i < len1; i++)
23              for(int j = 1; j < len2; j++)
24              {
25                  if(!i || !j) dp[i][j] = 0;
26                 else if(t1[i] == t2[j])
27                 {
28                     dp[i][j] = dp[i-1][j-1] + 1;
29                     path[i][j] = 3;
30                     //t1[i] == t2[j],则在(i,j)处得到lcs的一部分,标记此位置为3
31                 }
32                 else 
33                 {
34                     if(dp[i-1][j] > dp[i][j-1])
35                     {
36                         dp[i][j] = dp[i-1][j];
37                         path[i][j] = 1; 
38                     }
39                     else
40                     {
41                         dp[i][j] = dp[i][j-1];
42                         path[i][j] = 2;
43                     }
44                 }    
45             }
46             
47         //输出lcs : 
48         int len = dp[len1-1][len2-1]; //得到lcs的长度
49         int i = len1 - 1, j = len2 - 1;
50         while(len)
51         {
52             while(path[i][j] != 3)
53             {
54                 if(path[i][j] == 1) i--;
55                 else j--; 
56             }
57             lcs[len--] = t1[i];
58             i--, j--;
59         }
60         cout << lcs[1];
61         for(i = 2; i <= dp[len1-1][len2-1]; i++)
62             cout << ' ' << lcs[i];
63         puts("");    
64     }
65     return 0;
66 }
原文地址:https://www.cnblogs.com/cszlg/p/2912750.html