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

题意:  这个题目和书上的Lcs 问题差不多 但是他的元素不再是字符 
变成了 每个元素都是单词也就是字符串
方程:
LCS(i,j)表示数组以数组1的i个为止和以数组2的第j个为止的最长公共子序列,注意是第i个字符为子串的末尾字符(这样才能达到无后效性的目的)
1.LCS(i-1,j-1)+1 (a【i】=b【j】)
LCS(i,j)={
2.max{LCS(i-1,j),LCS(i,j-1)} (a【i】!=b【j】)
#include<iostream>
#include<cstring>
#include<string>
#include<vector>
using namespace std; vector<string> st1,st2;
int dp[][],path[][];
int tag; void printpath(int x,int y){
if(x==||y==) return;
if(path[x][y]==){
printpath(x-, y-);
if(tag==){
cout<<st1[x-];
tag=;
}
else cout<<" "<<st1[x-];
}
else if(path[x][y]==) printpath(x-, y);
else if(path[x][y]==)printpath(x, y-);
} int main(){
string str;
while(cin>>str){
st1.clear();st2.clear();
st1.push_back(str);
while(cin>>str&&str!="#") st1.push_back(str);
while(cin>>str&&str!="#") st2.push_back(str);
memset(dp,,sizeof(dp));
memset(path,,sizeof(path));
int len1=st1.size(),len2=st2.size();
tag=;
for(int i=;i<=len1;i++){
for(int j=;j<=len2;j++){
if(st1[i-]==st2[j-]){
dp[i][j]=dp[i-][j-]+;
path[i][j]=;
}else if(dp[i-][j]>dp[i][j-]){
dp[i][j]=dp[i-][j];
path[i][j]=;
}else{
dp[i][j]=dp[i][j-];
path[i][j]=;
}
}
}
printpath(len1, len2);
cout<<endl;
}
return ;
}

POJ 2250(最长公共子序列 变形)的更多相关文章

  1. Human Gene Functions POJ 1080 最长公共子序列变形

    Description It is well known that a human gene can be considered as a sequence, consisting of four n ...

  2. POJ 1458 最长公共子序列(dp)

    POJ 1458 最长公共子序列 题目大意:给出两个字符串,求出这样的一 个最长的公共子序列的长度:子序列 中的每个字符都能在两个原串中找到, 而且每个字符的先后顺序和原串中的 先后顺序一致. Sam ...

  3. poj 1080 Human Gene Functions (最长公共子序列变形)

    题意:有两个代表基因序列的字符串s1和s2,在两个基因序列中通过添加"-"来使得两个序列等长:其中每对基因匹配时会形成题中图片所示匹配值,求所能得到的总的最大匹配值. 题解:这题运 ...

  4. hdu1503 最长公共子序列变形

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1503 题意:给出两个字符串 要求输出包含两个字符串的所有字母的最短序列.注意输出的顺序不能 ...

  5. hdu 1080 dp(最长公共子序列变形)

    题意: 输入俩个字符串,怎样变换使其所有字符对和最大.(字符只有'A','C','G','T','-') 其中每对字符对应的值如下: 怎样配使和最大呢. 比如: A G T G A T G -  G ...

  6. POJ 1458 最长公共子序列

    子序列就是子序列中的元素是母序列的子集,且子序列中元素的相对顺序和母序列相同. 题目要求便是寻找两个字符串的最长公共子序列. dp[i][j]表示字符串s1左i个字符和s2左j个字符的公共子序列的最大 ...

  7. POJ 1458 最长公共子序列 LCS

    经典的最长公共子序列问题. 状态转移方程为 : if(x[i] == Y[j]) dp[i, j] = dp[i - 1, j - 1] +1 else dp[i, j] = max(dp[i - 1 ...

  8. hdu1243(最长公共子序列变形)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1243 分析:dp[i][j]表示前i个子弹去炸前j个恐怖分子得到的最大分.其实就是最长公共子序列加每个 ...

  9. 51Nod 1092 回文字符串 | 最长公共子序列变形

    求字符串和其逆的最长公共子序列,需要添加的字符数就为长度-最长公共子序列长 #include "stdio.h" #include "string.h" #de ...

随机推荐

  1. BZOJ 1607: [Usaco2008 Dec]Patting Heads 轻拍牛头

    1607: [Usaco2008 Dec]Patting Heads 轻拍牛头 Description   今天是贝茜的生日,为了庆祝自己的生日,贝茜邀你来玩一个游戏.     贝茜让N(1≤N≤10 ...

  2. Linux 各类软件整理汇总

    关于前端和后端的解释 详细链接见:http://wiki.ubuntu.org.cn/Qref/Apps Linux下程序通常不需要作为一个整体,而是模块化,于是有了可选的前端和后端——这种情况下:前 ...

  3. iOS 数据库第三方FMDB的简单使用

    一 FMDB的简单说明及介绍 FMDB的github地址 https://github.com/ccgus/fmdb FMDB是一款简洁的,易用的封装库,简单介绍一下FMDB的使用 在FMDB下载文件 ...

  4. Swift入门Hello World! Swift.

    苹果公司推出新的开发语言Swift,随着关于趋势,外观和OC什么是不一样的地方. 前提条件:已安装Xcode6-Beta(这个过程是不表) 1.打开Xcode6-Beta,第二选择Create a n ...

  5. c++ , const对象中的变量不能被修改

    const对象中的变量不能被修改,即使const对象中的函数也不能修改该对象中的变量值 #include <iostream> using namespace std; //------- ...

  6. 循环调用修正sic86

    create or replace procedure rebuild_sic86_wyl(pi_aac001 in number, po_fhz out varchar2, po_msg out v ...

  7. C语言--union关键字(转载)

    union维护足够的空间来放置多个数据成员中的“一种”,而不是为每一个数据成员配置空间.在union中,所有的数据成员共用一个空间,同一时间只能存储其中一个数据成员,所有的数据成员具有相同的起始地址.

  8. BZOJ 4177: Mike的农场( 最小割 )

    显然是最小割... 对于规律(i, j, k) i,j 互相连边, 容量为k 对于规则(S, a, b) 新建一个点x, x与S中每个点连一条弧, 容量+∞, 然后再根据a决定x与源点或汇点连边. 跑 ...

  9. uva 10330 - Power Transmission(网络流)

    uva 10330 - Power Transmission 题目大意:最大流问题. 解题思路:増广路算法. #include <stdio.h> #include <string. ...

  10. HDOJ 4007 Dave【最大覆盖集】

    Dave Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)Total Submis ...