UVA 531 Compromise(LCS算法+打印路径)
2015-03-18 18:47
369 查看
Compromise
Time Limit: 3000ms Memory Limit: 131072KB[PDF Link]
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
题目大意:
给你两段字符串,多行输入,单词之间空格隔开,每段以#号结束,要求输出这两段的最长的公共子序列。
题目大意:
LCS算法+打印路径,增加path【】【】数组。
代码:
#include<iostream> #include<string> #include<cstring> #include<vector> using namespace std; const int maxn = 105; string s1[maxn],s2[maxn]; int m,n,dp[maxn][maxn],path[maxn][maxn]; vector <string> answer; void LcsLength(){ memset(dp, 0, sizeof(dp)); memset(path, 0, sizeof(path)); for(int i=1;i<=m;i++){ for(int j=1;j<=n;j++){ if(s1[i-1]==s2[j-1]) { dp[i][j]=dp[i-1][j-1]+1; path[i-1][j-1]=1;//path[m] 没有被赋值,只跟新到了path[m-1][n-1],利用#号多读一个,就可以访问到最后一个单词了 } else{ if(dp[i-1][j]>=dp[i][j-1]) { dp[i][j]=dp[i-1][j]; path[i-1][j-1]=2; } else { dp[i][j]=dp[i][j-1]; path[i-1][j-1]=3; } } } } } void LCSPrint(int i,int j){ if(i<0||j<0) return ; if(path[i][j]==1){ LCSPrint(i-1,j-1); answer.push_back(s1[i]); } else if(path[i][j]==2){ LCSPrint(i-1,j); } else LCSPrint(i,j-1); } void output(){ for(int i=0;i<answer.size();i++){ if(i!=0) cout<<" "; cout<<answer[i]; } cout<<endl; } int main(){ int casen=0,r=0; while(cin>>s1[0]){ answer.clear(); for(int i=1;;i++){ cin>>s1[i]; m=i; if(s1[i]=="#") break; } for(int i=0;;i++){ cin>>s2[i]; n=i; if(s2[i]=="#") break; } LcsLength(); LCSPrint(m-1,n-1);//把#号丢掉 output(); } return 0; }
相关文章推荐
- UVA 531 DP LCS 打印路径
- UVA 531 - Compromise(dp + LCS打印路径)
- uva 531 - Compromise(LCS+打印路径)
- UVA 481 What Goes Up LIS+nlog(n)算法,打印路径
- UVA 531 - Compromise(dp + LCS打印路径)
- uva 11404 LCS打印字典序最小路径
- uva531- Compromise(lcs+打印路径)
- POJ 1141 / UVa 1626 Brackets Sequence (区间DP&打印路径)
- UVA 624 - CD (01背包打印路径)
- 最大流最小割算法; BFS搜索增广路径; 算法简单,打印结果也比较清晰;
- poj 2250 (LCS 需打印路径)
- POJ2250 - Compromise(LCS+打印路径)
- uva 11374 最短路+记录路径 好题 dijkstra优先队列优化算法 邻接表法 可做模板 G++提交
- uva624(01背包+打印路径)
- 微软等数据结构+算法面试100题(21)--二叉树打印到叶子节点的所有路径
- 【算法题】打印二元查找树中元素和等于指定数的所有路径
- 最长公共子序列 (LCS) 学习小记 Hdu 1159 + Poj 2250 (LCS路径打印)
- uva 624 CD (0-1背包打印路径)
- UVa 10054 - The Necklace, 欧拉回路+打印路径
- 【算法题】打印二元查找树中元素和等于指定数的所有路径