最长公共子序列 LCS
2015-08-15 15:54
316 查看
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87125#problem/D
题目:
Description
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
The input 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
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
Sample Output
题目:
Description
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
The input 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
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[i][j]为a[i]和b[j]的LCS长度,当a[i]=b[j]时,dp[i][j]=dp[i-1][j-1]+1; 否则dp[i][j]=max{dp[i-1][j],dp[i][j-1]}. 标记输出(就是要将每次的决策给记录下来,方便输出的时候用,每次输出的时候是当他们相等的时候决策就输出。)。
#include<iostream> #include<string> using namespace std; string a[105]; string b[105]; string c[105]; int dp[105][105],d[105][105]; int main() { int n,m,i,j; while(cin>>a[1]) { n=2; while(a[n-1]!="#") cin>>a[n++]; n-=2; cin>>b[1]; m=2; while(b[m-1]!="#") cin>>b[m++]; memset(dp,0,sizeof(dp)); memset(d,0,sizeof(d)); for(i=1;i<=n;i++) { for(j=1;j<=m;j++) { if(a[i]==b[j]) { dp[i][j]=dp[i-1][j-1]+1; d[i][j]=3;} //标记 else if(dp[i-1][j]>dp[i][j-1]) { dp[i][j]=dp[i-1][j];d[i][j]=1;} else {dp[i][j]=dp[i][j-1];d[i][j]=2;} } } int L=dp [m]; i=n; j=m; while(L) { while(d[i][j]!=3) { if(d[i][j]==1) i--; else j--; } L--; c[L]=a[i]; i--;j--; } cout<<c[0]; for(int k=1;k<dp [m];k++) cout<<" "<<c[k]; cout<<endl; } return 0; }
相关文章推荐
- 2015 HUAS Summer Contest#4~A
- android windowSoftInputMode说明
- 10.2-5 带哨兵的单向循环链表
- Android之——ContentProvider操作XML
- Butter knife的使用介绍与源码分析
- 归并排序 && 快速排序 ——分治思想
- 10.2-3 用单链表实现队列
- Linux学习笔记(21) Linux日志管理
- Linux学习笔记----rpm包and rpm命令管理
- 分享一款超好用的windows管理工具
- linux下常见目录作用
- 选择排序算法
- 介绍生产者-消费者模式
- Go语言入门
- 思考一下提高转码的速度
- protobuf 之 MessageLite 接口摘录
- 把握linux内核设计思想(十一):内存管理之页的分配与回收
- 不带哨兵的双向链表
- 数字二进制表示中1的个数
- BufferReader与BufferWriter实现socket通信注意事项