POJ 2250(LCS最长公共子序列)
compromise
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
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 题解:
题意:第一眼看,输入的东西有点长啊,仔细读了读,发现就是找到一个最长公共子序列并打印。
分析:找到最长公共子序列不难,难的是打印它的一条路径。我们知道是用二维数组dp来保存它的匹配数。一旦有匹配的它就会修改后面的值,保证了
如何一个状态当前的dp数据中是最大的值。为了记录它路径。我们需要用数组t来记录。可以用递归来实现。
可能这样说太抽象了,附上一张二维图。
ABCBDAB
BDCABA
两个序列,求最长公共子序列。图中就是路径回溯,这就是选择的过程,看懂了它再去看代码吧
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int t[][],dp[][];
string a[],b[];
void lcs(int x,int y)
{
for(int i=; i<=x; i++)
for(int j=; j<=y; j++)
{
if(a[i]==b[j])
{
dp[i][j]=dp[i-][j-]+;
t[i][j]=;
}
else
{
if(dp[i][j-]<=dp[i-][j])
{
dp[i][j]=dp[i-][j];
t[i][j]=;
}
else
{
dp[i][j]=dp[i][j-];
t[i][j]=;
}
}
}
} void output(int x,int y)
{
if(x==||y==) return;
if(t[x][y]==)
{
output(x-,y-);
cout<<a[x]<<" ";
}
else if(t[x][y]==)
output(x-,y);
else if(t[x][y]==)
output(x,y-);
} int main()
{
string s;
while(cin>>s)
{
int m=,n=;
a[]=s;
while(cin>>s&&s!="#")
{
a[m++]=s;
}
while(cin>>s&&s!="#")
{
b[n++]=s;
}
lcs(m,n);
output(m,n);
cout<<endl;
}
return ;
}
POJ 2250(LCS最长公共子序列)的更多相关文章
- POJ 1458 Common Subsequence(LCS最长公共子序列)
POJ 1458 Common Subsequence(LCS最长公共子序列)解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?c ...
- 算法设计 - LCS 最长公共子序列&&最长公共子串 &&LIS 最长递增子序列
出处 http://segmentfault.com/blog/exploring/ 本章讲解:1. LCS(最长公共子序列)O(n^2)的时间复杂度,O(n^2)的空间复杂度:2. 与之类似但不同的 ...
- 动态规划模板2|LCS最长公共子序列
LCS最长公共子序列 模板代码: #include <iostream> #include <string.h> #include <string> using n ...
- LCS 最长公共子序列
区别最长公共子串(连续) ''' LCS 最长公共子序列 ''' def LCS_len(x, y): m = len(x) n = len(y) dp = [[0] * (n + 1) for i ...
- POJ 1159 Palindrome(最长公共子序列)
Palindrome [题目链接]Palindrome [题目类型]最长公共子序列 &题解: 你做的操作只能是插入字符,但是你要使最后palindrome,插入了之后就相当于抵消了,所以就和在 ...
- LCS最长公共子序列(最优线性时间O(n))
这篇日志主要为了记录这几天的学习成果. 最长公共子序列根据要不要求子序列连续分两种情况. 只考虑两个串的情况,假设两个串长度均为n. 一,子序列不要求连续. (1)动态规划(O(n*n)) (转自:h ...
- LCS最长公共子序列
问题:最长公共子序列不要求所求得的字符串在所给字符串中是连续的,如输入两个字符串ABCBDAB和BDCABA,字符串BCBA和BDAB都是他们的公共最长子序列 该问题属于动态规划问题 解答:设序列X= ...
- LCS最长公共子序列HDU1159
最近一直在学习算法,基本上都是在学习动态规划以及字符串.当然,两者交集最经典之一则是LCS问题. 首先LCS的问题基本上就是在字符串a,b之间找到最长的公共子序列,比如 YAOLONGBLOG 和 Y ...
- LCS最长公共子序列~dp学习~4
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1513 Palindrome Time Limit: 4000/2000 MS (Java/Others ...
随机推荐
- [Locked] Generalized Abbreviation
Write a function to generate the generalized abbreviations of a word. Example:Given word = "wor ...
- CLOSE-UP FORMALWEAR_意大利进口_2015秋冬_男装发布会_西装图片系列_男装西装设计资料_WeArTrends时尚资讯网_国内最专业的服装设计资讯网站
CLOSE-UP FORMALWEAR_意大利进口_2015秋冬_男装发布会_西装图片系列_男装西装设计资料_WeArTrends时尚资讯网_国内最专业的服装设计资讯网站 CLOSE-UP FORMA ...
- poj1016
题目大意:数据统计 看明白了,就是给你一个数,例如31123314,代表的意思有3个1,1个2,3个3,1个4,但数字本身的有的数字也是有3个1,1个2,3个3,1个4,所以这样的数叫做self-in ...
- java MessageFormat 应用 和 疑惑
先来个demo String string = "{0}\"{1}\""; System.out.println(MessageFormat.format(st ...
- Android---用动画来处理布局的变化
本文译自:http://developer.android.com/training/animation/layout.html 布局动画一种系统预装的动画,每次布局配置发生变化时,系统会运行它.你所 ...
- android下大文件分割上传
由于android自身的原因,对大文件(如影视频文件)的操作很容易造成OOM,即:Dalvik堆内存溢出,利用文件分割将大文件分割为小文件可以解决问题. 文件分割后分多次请求服务. //文件分割上传 ...
- 微信支付bug
1.最基本的操作就是检查各项参数正确2.确保将测试微信号加入测试白名单 3.目录正确:发起授权请求的页面必须是在授权目录下的页面,而不能是存在与子目录中.否则会返回错误,Android返回“Syste ...
- Excel.Application手册
----转载:http://blog.csdn.net/xxfigo/article/details/6618129 定制模块行为(1) Option Explicit '强制对模块内所有变量进行声明 ...
- sql plus 和 pl/sql无法连接远程oracle数据库
前言:安装完oracle客户端后,可能会出现sql plus 和 pl/sql无法连接远程oracle数据库的情况,可能是以下原因: 针对sql plus连接不上: 1 可能原因:之前安装过oracl ...
- trie树信息抽取之中文数字抽取
这一章讲一下利用trie树对中文数字抽取的算法.trie树是一个非常有用的数据结构,可以应用于大部分文本信息抽取/转换之中,后续会开一个系列,对我在实践中摸索出来的各种抽取算法讲开来.比如中文时间抽取 ...