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 ...
随机推荐
- Java学习之IO之File类一
File的操作 package com.gh.file; import java.io.File; import java.io.IOException; /** * File操作 * @author ...
- centos下彻底删除MYSQL 和重新安装MYSQL
在Centos6.3上装了一个Mysql,结果mysql库被我玩丢了(这里面管理了mysql的权限).现在采用先彻底删除,然后重新安装Mysql. 1 删除Mysql yum remove mysq ...
- C# 方法的调用
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- PigCms 回复消息 "域名授权错误! 您使用的微信平台或源码为盗版"
本文地址:http://duwei.cnblogs.com/ Pigcms 将自动回复的API 写死了, 这里提供一个可用的API 在 PigCms/Lib/Action/Home/Weixinact ...
- Python 操作Redis
redis对比monoDB: redis和memcache 是key value非关系型数据库,mysql是关系型数据库,表的结构和保存的内容有严格的要求,关系型数据库无法保存临时数据或不标准的数据, ...
- 观django-messages包笔记
django_messages是一个提供注册用户之间互相发送消息的django app.最近在研究其实现机制,安装测试非常容易,导入包,配好url以及syncdb生成数据库即可使用. 一.收获一: 我 ...
- Week12(11月25日)
Part I:提问 =========================== 1.如何删除一条记录? Part II:理论学习 =========================== 到目前为止,我们考 ...
- 引用 xp系统引导修复(转载)
引用 3592wangxiaoxi 的 xp系统引导修复(转载) 原文来自百度知道a12424106关于“急需xp系统引导方面的知识!”的回复. XP系统的引导过程 如果想学习排除计算机系统故障,首先 ...
- android之IntentFilter的用法_Intent.ACTION_TIME_TICK在manifest.xml不起作用
在模仿一个天气预报的widget时候,用到了IntentFilter,感觉在manifest.xml注册的receiver跟用代码写registerReceiver()的效果应该是相同的,于是想证明一 ...
- 达内TTS6.0课件basic_day05