博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj_2250Compromise
阅读量:3530 次
发布时间:2019-05-20

本文共 3452 字,大约阅读时间需要 11 分钟。

Compromise
Time Limit:1000MS Memory Limit:65536K
Total Submissions:5074 Accepted:2314 Special Judge

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 landwirtesind fuer die abgeordneten ein buch mit sieben siegelnum dem abzuhelfenmuessen dringend alle subventionsgesetze verbessert werden#die steuern auf vermoegen und einkommensollten nach meinung der abgeordnetennachdruecklich erhoben werdendazu muessen die kontrollbefugnisse der finanzbehoerdendringend verbessert werden#

Sample Output

die einkommen der abgeordneten muessen dringend verbessert werden
 
最长公共子序列。用一个flag数组记录,输出时递归。
 
#include 
#include
#include
#include
using namespace std;string str[105];string dest[105];int dp[105][105];int flag[105][105];string result[105];int num;int LCS(int x, int y){ memset(dp,0,sizeof(dp)); memset(flag,-1,sizeof(flag)); int i,j; for(i=1; i<=x; i++) { for(j=1; j <= y; j++) { if(dest[i-1] == str[j-1]) { dp[i][j] = dp[i-1][j-1] + 1; flag[i][j] = 0; } else if(dp[i-1][j] >= dp[i][j-1]) { dp[i][j] = dp[i-1][j]; flag[i][j] = 1; } else { dp[i][j] = dp[i][j-1]; flag[i][j] = 2; } } } return dp[x][y];}void ShowLCS(int x, int y){ if(x == 0 || y == 0) return; if(flag[x][y] == 0) { ShowLCS(x-1,y-1); result[num++] = dest[x-1]; //cout<
<<" "; } else if(flag[x][y] == 1) { ShowLCS(x-1,y); } else { ShowLCS(x,y-1); }}int main(){ freopen("in.txt","r",stdin); string s; int dest_len,str_len,i; bool ans = false; while(cin >> s) { memset(dest,0,sizeof(dest)); memset(str,0,sizeof(str)); dest_len = 0; if(s != "#") { dest[dest_len++] = s; ans = false; while(cin >> s) { if(s == "#") { str_len = 0; while(cin >> s) { if(s == "#") { ans = true; break; } str[str_len++] = s; } } else { dest[dest_len++] = s; } if(ans == true) break; } } int ant = LCS(dest_len,str_len); num = 0; ShowLCS(dest_len,str_len); //cout<
<

转载地址:http://rtyhj.baihongyu.com/

你可能感兴趣的文章
[LeetCode javaScript] 179. 最大数
查看>>
[LeetCode javaScript] 56. 合并区间
查看>>
[LeetCode javaScript] 190. 颠倒二进制位
查看>>
[LeetCode javaScript] 521. 最长特殊序列 Ⅰ
查看>>
[LeetCode javaScript] 806. 写字符串需要的行数
查看>>
[LeetCode javaScript] 868. 二进制间距
查看>>
[LeetCode javaScript] 824. 山羊拉丁文
查看>>
[LeetCode javaScript] 463. 岛屿的周长
查看>>
[LeetCode javaScript] 107. 二叉树的层次遍历 II
查看>>
[LeetCode javaScript] 637. 二叉树的层平均值
查看>>
[LeetCode javaScript] 1. 两数之和
查看>>
[LeetCode javaScript] 14. 最长公共前缀
查看>>
[LeetCode javaScript] 26. 删除排序数组中的重复项
查看>>
[LeetCode javaScript] 8. 字符串转换整数 (atoi)
查看>>
[LeetCode javaScript] 28. 实现strStr()
查看>>
cv2.error: OpenCV(3.4.2) c:\projects\opencv-python\opencv\modules\imgproc\src\color.hpp:25
查看>>
前端网页学习7(css背景属性)
查看>>
前端网页学习8(css三大特性:层叠性,继承性,优先级)
查看>>
前端网页学习9(css盒子)
查看>>
python学习8(列表)
查看>>