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. 基于visual Studio2013解决C语言竞赛题之0203格式化输出

     题目 解决代码及点评 #include <stdio.h> #include <stdlib.h> void main() { // print是输出函数,参数%s表示输 ...

  2. boost::thread用法

    最近在做一个消息中间件里面涉及到多线程编程,由于跨平台的原因我采用了boost线程库.在创建线程时遇到了几种线程创建方式现总结如下: 首先看看boost::thread的构造函数吧,boost::th ...

  3. poj 2346 Lucky tickets(区间dp)

    题目链接:http://poj.org/problem?id=2346 思路分析:使用动态规划解法:设函数 d( n, x )代表长度为n且满足左边n/2位的和减去右边n/2位的和为x的数的数目. 将 ...

  4. Visual Studio 创建和使用dll的方法

    DLL是一个包含可由多个程序同时使用的代码和数据的库. DLL文件就是把一些函数导出来,然后在新程序中进行复用的过程. 第一部分:使用Visual Studio 2010进行DLL的制作 生成方法一: ...

  5. CentOS下安装两个或多个Tomcat7

    链接地址:http://lcbk.net/tomcat/1407.html 首先安装JDK 安装之前检查下是否已经安装了openJDK,如果已安装,建议用yum remove 卸载掉. [root@b ...

  6. struts2总结【转载】

    1,struts2的form表单里面和url里面的传值以及Action所继承的父类都可以自动set属性注入action中,及继承的action中. 2,凡是url和form表单传值,在action方法 ...

  7. 一步一步重写 CodeIgniter 框架 (11) —— 使用 CodeIgniter 函数库

    在完成了CI框架的类库扩展后,很自然我们就会想到函数库的扩展.函数库的扩展在 CI 中称为 helper 函数与类有不同的地方,它不能继承,只能覆盖或者添加新的函数,或者直接完全新定义的一组函数. 由 ...

  8. UVA 10340 (13.08.25)

    Problem E All in All Input: standard input Output: standard output Time Limit: 2 seconds Memory Limi ...

  9. [Unity 3D] Unity 3D 性能优化 (一)

    听到过很多用Unity 3D开发游戏的程序员抱怨引擎效率太低,资源占用太高,包括我自己在以往项目的开发中也头疼过.最近终于有了空闲,可以仔细的研究一下该如何优化Unity 3D下的游戏性能.其实国外有 ...

  10. Python获取当地的天气和随意城市的天气

    先从中国天气网得到数据('http://www.weather.com.cn/data/cityinfo/'+城市编码),每一个城市都有各自的编码,怎样得到用户所在地的城市编码呢?用一个网页就是专门干 ...