PKU 2250(变形的LCS)
2010-08-13 16:31
309 查看
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 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
Sample Output
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 2991 | Accepted: 1407 | Special Judge |
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
#include <stdio.h> #include <string.h> const int N = 101; const int M = 32; int longest ; char s1 [M],s2 [M],node [M]; int len1,len2; int ll ; void LCS() { memset(ll,0,sizeof(ll)); int i,j,k,flag; longest[0][0] = 0; for(i=1;i<=len1;i++) longest[i][0] = 0; for(i=1;i<=len1;i++) longest[0][i] = 0; for (i=1;i<=len1;i++) { for (j=1;j<=len2;j++) { if(strcmp(s1[i-1],s2[j-1]) == 0) { longest[i][j] = longest[i-1][j-1]+strlen(s1[i-1]); for(k = 0;k < ll[i - 1][j - 1];k++) { strcpy(node[i][j][ll[i][j]++],node[i-1][j-1][k]); } strcpy(node[i][j][ll[i][j]++],s1[i-1]); } else if(longest[i-1][j]>longest[i][j-1]) { longest[i][j] = longest[i-1][j]; for(k = 0;k < ll[i - 1][j];k++) { strcpy(node[i][j][ll[i][j]++],node[i-1][j][k]); } } else { longest[i][j] = longest[i][j-1]; for(k = 0;k < ll[i][j-1];k++) { strcpy(node[i][j][ll[i][j]++],node[i][j-1][k]); } } } } flag = 0; for(i=0;i<ll[len1][len2];i++) { if(flag == 1) printf(" "); printf("%s",node[len1][len2][i]); flag = 1; } printf("/n"); } int main() { while (scanf("%s",s1[0])!=EOF) { len1 = 0; while (strcmp(s1[len1],"#") != 0) { scanf("%s",s1[++len1]); } scanf("%s",s2[0]); len2 = 0; while (strcmp(s2[len2],"#") != 0) { scanf("%s",s2[++len2]); } LCS(); } return 0; }
相关文章推荐
- pku 2250 Compromise(LCS)
- pku 1159 Palindrome DP LCS 变形
- Pku oj 2250 Compromise(LCS+打印路径)
- pku2250--Compromise(最长公共子串,记录结果)
- Codeforces 463D Gargari and Permutations DP(LCS变形)
- poj 2250 Compromise dp lcs 路径输出
- poj 2250/3356 LCS(文章LCS/AGTC)
- poj 2250 Compromise 【最长公共子序列变形】
- POJ1080Human Gene Functions(LCS变形)
- HZAUoj 1015: LCS 【LCS变形】 + 1016: Array C 【贪心】
- poj 3356 AGTC(lcs 变形题)
- poj1080--Human Gene Functions(dp:LCS变形)
- hdu1243反恐训练营(LCS最长公共子序列变形)
- hdoj 5495 LCS 【统计元素个数大于1的置换群数目】【LCS变形】
- HDU 1087 lcs变形 (简单DP)
- poj 1080 (LCS变形)
- POJ 1080-Human Gene Functions(LCS变形-最大得分)
- 动态规划(LCS)(POJ 2250 Compromise)
- CodeForces - 346B -LCS变形+dfs+KMP
- pku 1179 polygon 石子合并的变形题dp 解题报告