POJ 2250(最长公共子序列 变形)
Description
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
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
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(最长公共子序列 变形)的更多相关文章
- Human Gene Functions POJ 1080 最长公共子序列变形
Description It is well known that a human gene can be considered as a sequence, consisting of four n ...
- POJ 1458 最长公共子序列(dp)
POJ 1458 最长公共子序列 题目大意:给出两个字符串,求出这样的一 个最长的公共子序列的长度:子序列 中的每个字符都能在两个原串中找到, 而且每个字符的先后顺序和原串中的 先后顺序一致. Sam ...
- poj 1080 Human Gene Functions (最长公共子序列变形)
题意:有两个代表基因序列的字符串s1和s2,在两个基因序列中通过添加"-"来使得两个序列等长:其中每对基因匹配时会形成题中图片所示匹配值,求所能得到的总的最大匹配值. 题解:这题运 ...
- hdu1503 最长公共子序列变形
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1503 题意:给出两个字符串 要求输出包含两个字符串的所有字母的最短序列.注意输出的顺序不能 ...
- hdu 1080 dp(最长公共子序列变形)
题意: 输入俩个字符串,怎样变换使其所有字符对和最大.(字符只有'A','C','G','T','-') 其中每对字符对应的值如下: 怎样配使和最大呢. 比如: A G T G A T G - G ...
- POJ 1458 最长公共子序列
子序列就是子序列中的元素是母序列的子集,且子序列中元素的相对顺序和母序列相同. 题目要求便是寻找两个字符串的最长公共子序列. dp[i][j]表示字符串s1左i个字符和s2左j个字符的公共子序列的最大 ...
- 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 ...
- hdu1243(最长公共子序列变形)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1243 分析:dp[i][j]表示前i个子弹去炸前j个恐怖分子得到的最大分.其实就是最长公共子序列加每个 ...
- 51Nod 1092 回文字符串 | 最长公共子序列变形
求字符串和其逆的最长公共子序列,需要添加的字符数就为长度-最长公共子序列长 #include "stdio.h" #include "string.h" #de ...
随机推荐
- Hdu 2364 Escape
Problem地址:http://acm.hdu.edu.cn/showproblem.php?pid=2364 这道题的特殊之处在于能转弯时不能直走,必须转弯,所以在行走时,要判断能否转弯,不能转弯 ...
- BZOJ 1828
program bzoj1828; ; check=; type node=record l,r,s,a:longint; end; ..maxn*] of node; a,b,c:..maxn] o ...
- GitHub学习笔记
安装 Ubuntu上安装Git sudo apt-get install git Windows上安装Git msysgit是Windows版的Git.从http://msysgit.github.i ...
- ios常见加密解密方法
在其他平台中经常会计算MD5值,在iOS平台中也提供了该方法,首先需要导入头文件 #import <CommonCrypto/CommonDigest.h> 方法CC_MD5可以获取MD5 ...
- postgres-xc手册生成方法
步骤 检测编译环境 安装编译工具 编译 以上只在linux环境当中进行,本人所用系统ubuntu15.04 检测编译环境 在posgtgresql目录下运行./configure,并安装需要安 ...
- c++隐藏实例
隐藏:是指派生类的函数屏蔽了与其同名的基类函数,规则如下:(1)如果派生类的函数与基类的函数同名,但是参数不同.此时,不论有无virtual关键字,基类的函数将被隐藏(注意别与重载混淆). 很简单略去 ...
- ContentProvider的一些总结
ContentProvider中的URI, The URI that identifies the provider 一个特定的uri对应着唯一一个内容提供者, 谷歌官方文档里的说明, Query ...
- js_DOM属性
.nodeType==1,指的是li, .nodeType==3,则指的文本节点. .children属性,和 .childNodes属性类似,但是只会包含元素节点,而不会包含文本节点. .child ...
- <转> 30 个有关 Python 的小技巧
目录[+] 1.1 拆箱 1.2 拆箱变量交换 1.3 扩展拆箱(只兼容python3) 1.4 负数索引 1.5 切割列表 1.6 负数索引切割列表 1.7指定步长切割列表 1.8 负数步长切割列表 ...
- 为什么 Flask 有那么多的好评?
著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处.作者:松鼠奥利奥链接:http://www.zhihu.com/question/28902969/answer/42530571来 ...