首页 > 代码库 > POJ 2250 Compromise
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.
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.
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 landwirtesind fuer die abgeordneten ein buch mit sieben siegelnum dem abzuhelfenmuessen dringend alle subventionsgesetze verbessert werden#die steuern auf vermoegen und einkommensollten nach meinung der abgeordnetennachdruecklich erhoben werdendazu muessen die kontrollbefugnisse der finanzbehoerdendringend 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 else33 {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 else55 {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 }
POJ 2250 Compromise
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。