您的位置:首页 > 其它

uva531Compromise (最长公共子序列+路径输出)

2015-07-24 17:14 381 查看
Compromise

Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu

Submit Status Practice UVA 531

Appoint description:

Description

Download as PDF

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 Specification

The input file 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 Specification

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

dp的路径输出,其实就是dp规划方向的逆过程,dp过程保存着每一个状态,只要在dp的同时,用一个数组把这个状态转移的方向给标记下来,然后就可以沿着标记的反方向回溯回去,这样就可以输出这个路径

#include<iostream>
#include<algorithm>
#include<map>
#include<cstdio>
#include<cstdlib>
#include<vector>
#include<cmath>
#include<cstring>
#include<stack>
#include<string>
#include<set>
#include<fstream>
using namespace std;
#define pb push_back
#define cl(a,b) memset(a,b,sizeof(a))
#define bug printf("===\n");
#define rep(a,b) for(int i=a;i<b;i++)
#define rep_(a,b) for(int i=a;i<=b;i++)
#define P pair<int,int>
#define X first
#define Y second
#define vi vector<int>
const int maxn=105;
const int inf=999999999;
typedef long long LL;

int a[maxn];
int b[maxn];
char tmp[maxn];
int dp[maxn][maxn];//dp状态数组
int f[maxn][maxn];//保存规划方向的数组
map<string,int> mp;//单词转化为一个整数
map<int,string> ans;//整数对应的单词
int cnt;
int getid(string s){
if(mp.count(s)){
return mp[s];
}
mp[s]=cnt;
ans[cnt]=s;
cnt++;
return cnt-1;
}
int sum;

void print(int i,int j){//路径输出
if(i<0||j<0){
return ;
}
if(f[i][j]==2){
print(i-1,j-1);
cout<<ans[a[i-1]];
if(--sum)putchar(' ');
else puts("");
}
if(f[i][j]==1){
print(i-1,j);
}
if(f[i][j]==-1){
print(i,j-1);
}

}

int main(){
while(true){
int cnt=0;
mp.clear();
ans.clear();
int i=0;
int state=0;
while(true){
state=scanf("%s",tmp);
if(state==-1)break;
if(strcmp(tmp,"#")==0)break;
int id=getid(tmp);
a[i++]=id;
}
if(state==-1)return 0;
int n=i;
i=0;
while(true){
scanf("%s",tmp);
if(strcmp(tmp,"#")==0)break;
int id=getid(tmp);
b[i++]=id;
}
int m=i;
cl(dp,0);
cl(f,0);
for(int i=0; i<n; i++){
for(int j=0; j<m; j++){
if(a[i]==b[j]){
dp[i+1][j+1]=dp[i][j]+1;
f[i+1][j+1]=2;
}
else{
if(dp[i+1][j]<dp[i][j+1]){
dp[i+1][j+1]=dp[i][j+1];
f[i+1][j+1]=1;
}
else {
dp[i+1][j+1]=dp[i+1][j];
f[i+1][j+1]=-1;
}
}
}
}
sum=dp
[m];
if(sum!=0)
print(n,m);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: