POJ 2250 Compromise

Compromise

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6654   Accepted: 2976   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
题意:给出两段文字,求出最长的公共单词串
思路:LCS问题,只需要开个二维来记录就好了
 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <algorithm>
 4 using namespace std;
 5
 6 char a[35][105],b[35][105],c[35][105];
 7 int dp[105][105],mark[105][105],len1,len2,cnt;
 8
 9 void LCS()
10 {
11     int i,j;
12     memset(dp,0,sizeof(dp));
13     memset(mark,0,sizeof(mark));
14     for(i = 0;i<=len1;i++)
15     mark[i][0] = 1;
16     for(i = 0;i<=len2;i++)
17     mark[0][i] = -1;
18     for(i = 1; i<=len1; i++)
19     {
20         for(j = 1; j<=len2; j++)
21         {
22             if(!strcmp(a[i-1],b[j-1]))
23             {
24                 dp[i][j] = dp[i-1][j-1]+1;
25                 mark[i][j] = 0;
26             }
27             else if(dp[i-1][j]>=dp[i][j-1])
28             {
29                 dp[i][j] = dp[i-1][j];
30                 mark[i][j] = 1;
31             }
32             else
33             {
34                 dp[i][j] = dp[i][j-1];
35                 mark[i][j] = -1;
36             }
37         }
38     }
39 }
40
41 void PrintLCS(int i,int j)
42 {
43     if(!i&&!j)
44         return ;
45     if(mark[i][j]==0)
46     {
47         PrintLCS(i-1,j-1);
48         strcpy(c[cnt++],a[i-1]);
49     }
50     else if(mark[i][j]==1)
51     {
52         PrintLCS(i-1,j);
53     }
54     else
55     {
56         PrintLCS(i,j-1);
57     }
58 }
59
60 int main()
61 {
62     int i;
63     while(~scanf("%s",a[0]))
64     {
65         len1 = 1;
66         while(strcmp(a[len1-1],"#"))
67             scanf("%s",a[len1++]);
68         len1-=1;
69         scanf("%s",b[0]);
70         len2 = 1;
71         while(strcmp(b[len2-1],"#"))
72             scanf("%s",b[len2++]);
73         LCS();
74         cnt = 0;
75         PrintLCS(len1,len2);
76         printf("%s",c[0]);
77         for(i = 1; i<cnt; i++)
78         {
79             printf(" %s",c[i]);
80         }
81         printf("\n");
82     }
83
84
85     return 0;
86 }
 
时间: 2024-10-04 20:08:27

POJ 2250 Compromise的相关文章

POJ 2250 Compromise (DP,最长公共子序列)

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

周赛 POJ 2250 Compromise

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 Germa

poj 2250 Compromise dp lcs 路径输出

点击打开链接题目链接 Compromise Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6520   Accepted: 2922   Special Judge Description In a few months the European Currency Union will become a reality. However, to join the club, the Maastricht criteria

LIS POJ 2250 Compromise

题目传送门 1 /* 2 LIS模板题:题目看错了,是求单词的最长上升子序列! 3 编程好累:) 4 */ 5 #include <cstdio> 6 #include <iostream> 7 #include <cstring> 8 #include <algorithm> 9 #include <string> 10 using namespace std; 11 12 const int MAXN = 1e2 + 10; 13 const

POJ 2250 Compromise (线性dp LCS +递归路径)

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

POJ 2250 Compromise(最长公共子序列LCS)

http://poj.org/problem?id=2250 题意: 给你两段由空格分隔的语句, 要你求该两段语句的最长公共子序列. 且随便输出一个解即可. 注意每个单词需要看成我们一般处理字符串子序列的一个单独字符. 即每个单词是一个整体. 分析: 与往常计算最长公共子序列一样的方式即可. 然后用DFS输出序列即可.本题与POJ1458提供的解法本质一样. http://blog.csdn.net/u013480600/article/details/40741333 AC代码: #inclu

POJ 2250 Compromise(最长公共子序列)

题意:求两段文本的最长公共文本: 思路:最长公共子序列+打印公共序列: #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int dp[505][505],num1,num2; char s[505][505],s1[505][505],s2[505][505]; void lcs(int a,int b) { if(a==0||b==0) return; if(s[a

poj 2250 Compromise (LCS)

题目大意:给出两段文字,求出最长的公共单词串. 直接是以前的代码改一点就A了. #include <stdio.h> #include <string.h> #include <algorithm> using namespace std; char s1[35][100],s2[35][100],s[35][100]; int len1,len2,dp[105][105],mark[105][105],l; void LCS() { int i,j; memset(d

POJ 2250 Compromise (UVA 531)

LCS问题,基础DP. 让我很忧伤的WA了很多次.只是一个LCS问题,需要记录一下路径. 自己的想办法记录path出错,最后只好用标记. 没有什么优化,二维数组,递归打印,cin.eof() 来识别 end of file 标识. 至于单词用map 映射的.其实也用不着,直接二维string或者 二维char 然后strcmp 也行. Special Judge 交 UVA 531 奇怪的PE了... 然后改成 flag 标记 输出 空格.终于都AC了. #include<cstdio> #i