UVa 531 Compromise
2014-02-25 21:18
323 查看
Compromise |
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
最长公共子序列问题,不过这次不只要求出长度,还要写出最长公共子序列。
用两个数组pos_a[i][j]和pos_b[i][j]记录每次转移的动向,比如dp[i][j]是由dp[i-1][j]转移过来的,那么pos_a[i][j]=i-1,pos_b[i][j]=j。
求解完最长公共子序列之后,可以从末尾一点一点找回去,当找到x=pos_a[i][j]和y=pos_b[i][j]时,若字符串a[x]==b[y],那么就将这个字符串记录下来,最后将所有记录下来的字符串倒序打印出来即可
#include<iostream> #include<string> #include<cstring> #include<stack> #include<cstdio> using namespace std; string a[105],b[105]; int dp[105][105],pos_a[105][105],pos_b[105][105]; int main() { while(cin>>a[1]) { int p,q; for(p=1;a[p]!="#";p++) cin>>a[p+1]; cin>>b[1]; for(q=1;b[q]!="#";q++) cin>>b[q+1]; memset(dp,0,sizeof(dp)); memset(pos_a,-1,sizeof(pos_a)); memset(pos_b,-1,sizeof(pos_b)); for(int i=1;i<p;i++) for(int j=1;j<q;j++) { if(a[i]==b[j]) { dp[i][j]=dp[i-1][j-1]+1; pos_a[i][j]=i-1; pos_b[i][j]=j-1; } else { if(dp[i-1][j]>dp[i][j-1]) { dp[i][j]=dp[i-1][j]; pos_a[i][j]=i-1; pos_b[i][j]=j; } else { dp[i][j]=dp[i][j-1]; pos_a[i][j]=i; pos_b[i][j]=j-1; } } } int x=p-1; int y=q-1; stack<string> s; while(x>0&&y>0) { if(a[x]==b[y]) s.push(a[x]); int temp=x; x=pos_a[x][y]; y=pos_b[temp][y]; } while(s.size()>1) { cout<<s.top()<<' '; s.pop(); } cout<<s.top()<<endl; } return 0; }
[C++]
相关文章推荐
- C语言宏定义函数的使用(定义单行和多行)
- Spring框架启动Tomcat报错
- JS兼容性总结
- 安装Quartus_II_9.0
- Struts2复习之OGNL(1)
- android客户端和服务器实现传图片和文件
- iOS 关于objectForKey返回类型是不是mutable
- LeetCode - 3Sum Closest
- SVN安装与配置
- NSCalendar
- 界面设计之尺寸篇
- Opencv大津(otsu)的使用代码
- 1137. 河床 变笨了
- python 科学计算学习一:numpy快速处理数据(1)
- mysql字段类型范围说明:int、bigint、smallint、tinyint,char、varchar、nvarchar
- 关于从java转C++的想好了
- 线程互斥和同步
- AutoMapper在MVC中的运用06
- 【Java核心技术——反射
- hdu3177 贪心