第二次做,不过一开始没有看清会是多组数据,都搞得我都不知道哪错了。
这个和找公共子序列是一个道理(把一个单词看成是一个字母)。输出的路径进行记录,pre[][],其中3代表是左上,2代表左边,1代表正上方
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<vector>
#include<cmath>
#include<set>
#include<cstdlib>
#include<cstring>
#include<stack>
#include<string>
using namespace std;
//freopen("C://i.txt","r",stdin);
#define N 1001
char a[111][33];
char b[111][33];
int lena,lenb;
int dp[111][111];
int
pre[111][111];
bool f;
void put(int i,int j)
{
// cout<<i<<' '<<j<<' '<<pre[i][j]<<endl;
int k;
if (i==0||j==0)
return ;
if (pre[i][j]==3)
put(i-1,j-1);
else if (pre[i][j]==1)
put(i,j-1);
else if (pre[i][j]==2)
put(i-1,j);
if (strcmp(a[i],b[j])==0) //这里要加强i,j不然有空格
{
if (f)
cout<<' ';
f=true;
cout<<a[i];
}
}
int main()
{
freopen("C://i.txt","r",stdin);
int i,j,k;
while (cin>>a[1])
{
lena=2;
f=false;
while (cin>>a[lena])
{
//cout<<a[lena]<<endl;
if (a[lena][0]=='#')
break;
else
lena++;
}
lena--;
lenb=1;
while (cin>>b[lenb])
{
//cout<<b[lenb]<<endl;
if (b[lenb][0]=='#')
break;
else
lenb++;
}
lenb--;
int ansi,ansj;
ansi=0;
ansj=0;
memset(pre,0,sizeof(pre));
for (i=0;i<=lena;i++)
{
for (j=0;j<=lenb;j++)
{
if (i==0||j==0)
{
dp[i][j]=0;
}
else if (strcmp(a[i],b[j])==0)
{
// cout<<i<<' '<<j<<endl;
dp[i][j]=dp[i-1][j-1]+1;
pre[i][j]=3;
}
else
{
if (dp[i-1][j]>dp[i][j-1])
{
dp[i][j]=dp[i-1][j];
pre[i][j]=2;
}
else
{
dp[i][j]=dp[i][j-1];
pre[i][j]=1;
}
}
}
}
// cout<<dp[lena][lenb]<<endl;
// put(lena,lenb);
char ans[111][33];
k=0;
put(lena,lenb);
cout<<endl;
/*
i=lena;
j=lenb;
while (pre[i][j])
{
int q,p;
q=i;
p=j;
if (pre[i][j]==3)
p--,q--;
else if (pre[i][j]==2)
q--;
else
p--;
if (dp[i][j]!=dp[q][p])
strcpy(ans[k++],a[i]);
i=q;
j=p;
}
for (i=k-1;i>=0;i--)
printf("%s ",ans[i] );
printf("\n");
*/
}
}
Compromise
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 4814 | Accepted: 2205 | 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