Compromise
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 8017 | Accepted: 3479 | 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 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
题目大意:
给你两段文字,让你求其最长公共子序列的任意解,并且输出其路径。要求输出正序解。
思路:
1、设定dp【i】【j】表示第一个字符串以第i位结尾,第二个字符串以第j位结尾的最长公共子序列长度。
2、辣么状态转移方程:
①if(a【i】==b【j】)dp【i】【j】=dp【i-1】【j-1】+1;
②else dp【i】【j】=max(dp【i-1】【j】,dp【i】【j-1】);
3、设定tmp=dp【n1】【n2】,然后逆序找解。if(a【i】==b【j】&&tmp==dp【i】【j】)tmp--,并且记录下当前字符串,最终逆序输出即可。
Ac代码:
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
char a[105][35];
char b[105][35];
int dp[105][105];
char ans[105][35];
int main()
{
memset(a,'0',sizeof(a));
memset(b,'0',sizeof(b));
while(~scanf("%s",a[0]))
{
int cont=1;
memset(dp,0,sizeof(dp));
while(1)
{
scanf("%s",a[cont++]);
if(strcmp(a[cont-1],"#")==0)break;
}
int cont2=0;
while(1)
{
scanf("%s",b[cont2++]);
if(strcmp(b[cont2-1],"#")==0)break;
}
cont--;
cont2--;
for(int i=1;i<=cont;i++)
{
for(int j=1;j<=cont2;j++)
{
if(strcmp(a[i-1],b[j-1])==0)
{
dp[i][j]=dp[i-1][j-1]+1;
}
else
{
dp[i][j]=max(dp[i][j-1],dp[i][j]);
dp[i][j]=max(dp[i-1][j],dp[i][j]);
}
}
}
int contz=0;
int tmp=dp[cont][cont2];
for(int i=cont;i>=0;i--)
{
for(int j=cont2;j>=0;j--)
{
if((strcmp(a[i-1],b[j-1])==0)&&tmp==dp[i][j])
{
tmp--;
strcpy(ans[contz++],a[i-1]);
}
}
}
reverse(ans,ans+contz);
for(int i=0;i<contz-1;i++)
{
printf("%s ",ans[i]);
}
printf("%s\n",ans[contz-1]);
}
}