uva531Compromise (最长公共子序列+路径输出)
来源:互联网 发布:淘宝电动车便宜 编辑:程序博客网 时间:2024/06/06 15:01
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[n][m]; if(sum!=0) print(n,m); } return 0;}
- uva531Compromise (最长公共子序列+路径输出)
- 最长公共子序列问题(要求输出路径)
- POJ2250---Compromise(dp,最长公共子序列,输出路径)
- 输出最长公共子序列
- 输出最长公共子序列
- 输出最长公共子序列
- 输出最长公共子序列
- HDU-1503(最长公共子序列+路径跟踪)
- 最长公共子序列+打印路径
- 最长公共子序列打印路径
- 最长公共子序列 路径还原
- poj_2250 Compromise(输出最长公共子序列)
- 输出所有的最长公共子序列
- 输出最长公共子序列字母表顺序
- **输出最长公共子序列-字符串
- 最长公共子序列及其输出
- 输出所有的最长公共子序列
- 最长公共上升子序列(LCIS)学习小记 Hdu 1423 + Poj 2127 (LCIS路径输出)
- 根据用户权限和角色拼接菜单
- java.io.IOException: Tried to send an out-of-range integer as a 2-byte value
- mysql优化limit查询语句的5个方法
- 【生活】这么近,那么远
- org.eclipse.jdt.internal.compiler.CompilationResult.getProblems()[Lorg/eclipse/jdt/core/comp
- uva531Compromise (最长公共子序列+路径输出)
- NOJ_1017 乘积最大(DP大法)
- VC编译连接选项详解
- ios UITableView代码
- Dedecms 火车采集器免登陆发布接口,自动生成上下篇、栏目页、首页
- linux下,让程序在后台运行
- cocos2d-x中有一个JniHelper类详细使用
- Online Judge System For SzNOI 题库 语法百题 c++ d024
- Leetcode:Two Sum