In a few months the European Currency Union will become a reality. However, to join the club, the Maastricht criteria must be ful lled, and this is not a trivial task for the countries (maybe except for Luxembourg). To enforce that Germany will ful ll 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 le 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 there will be at least one common word in each text. Each text will be terminated by a line containing a single ‘#’.
Input is terminated by end of le.
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 <iostream>
#include <cstring>
#include <stack>
using namespace std;
const int Maxn=105;
string s1[Maxn],s2[Maxn];
int pos1[Maxn][Maxn],pos2[Maxn][Maxn];
int f[Maxn][Maxn];
stack <string> result;
int main()
{
while(cin>>s1[1])
{
int m=2,n=1;
while(cin>>s1[m])
{
if(s1[m]=="#")
{
m--;
break;
}
m++;
}
while(cin>>s2[n])
{
if(s2[n]=="#")
{
n--;
break;
}
n++;
}
/*while(!result.empty())
{
result.pop();
}*/
memset(f,0,sizeof(f));
memset(pos1,0,sizeof(pos1));
memset(pos2,0,sizeof(pos2));
for(int i=1;i<=m;i++)
{
for(int j=1;j<=n;j++)
{
if(s1[i]==s2[j])
{
f[i][j]=f[i-1][j-1]+1;
pos1[i][j]=i-1;
pos2[i][j]=j-1;
}
else
{
if(f[i][j-1]>f[i-1][j])
{
f[i][j]=f[i][j-1];
pos1[i][j]=i;
pos2[i][j]=j-1;
}
else
{f[i][j]=f[i-1][j];
pos1[i][j]=i-1;
pos2[i][j]=j;
}
}
}
}
while(m&&n)
{
if(s1[m]==s2[n])
result.push(s1[m]);
int k=m;
m=pos1[m][n];
n=pos2[k][n];
}
cout<<result.top();
result.pop();
while(!result.empty())
{
cout<<" "<<result.top();
result.pop();
}
cout<<endl;
/*while(result.size() > 1)
{
cout << result.top() << " ";
result.pop();
}
cout << result.top()<<endl;*/
}
return 0;
}
或用递归输出
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn = 110;
const int INF = 0x3f3f3f3f;
string a[maxn], b[maxn];
int d[maxn][maxn], p[maxn][maxn], f = 0;
void print(int x, int y)
{
if(!x || !y) return ;
if(p[x][y] == 1) {
print (x-1, y-1);
if(f) printf(" ");
else f = 1;
cout << a[x];
}
else if(p[x][y] == 0) print (x-1, y);
else print (x, y-1);
}
int main()
{
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
while (cin >> a[1]) {
int cnt = 2, cnt2 = 1;
while (cin >> a[cnt] && a[cnt][0] != '#') cnt ++;
while (cin >> b[cnt2] && b[cnt2][0] != '#') cnt2 ++;
memset (d,0,sizeof(d));
memset (p,0,sizeof(p));
for (int i = 1; i < cnt; i ++) {
for (int j = 1; j < cnt2; j ++) {
if(a[i] == b[j]) {
d[i][j] = d[i-1][j-1] + 1;
p[i][j] = 1;
}
else if (d[i-1][j] >= d[i][j-1]) {
d[i][j] = d[i-1][j];
p[i][j] = 0;
}
else {
d[i][j] = d[i][j-1];
p[i][j] = -1;
}
}
}
f = 0;
print (cnt-1, cnt2-1);
printf ("\n");
}
return 0;
}