最长公共子序列 LCS
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87125#problem/D
题目:
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[i][j]为a[i]和b[j]的LCS长度,当a[i]=b[j]时,dp[i][j]=dp[i-1][j-1]+1;
否则dp[i][j]=max{dp[i-1][j],dp[i][j-1]}.
标记输出(就是要将每次的决策给记录下来,方便输出的时候用,每次输出的时候是当他们相等的时候决策就输出。)。
#include<iostream>
#include<string>
using namespace std;
string a[];
string b[];
string c[];
int dp[][],d[][];
int main()
{
int n,m,i,j;
while(cin>>a[])
{
n=;
while(a[n-]!="#")
cin>>a[n++];
n-=;
cin>>b[];
m=;
while(b[m-]!="#")
cin>>b[m++];
memset(dp,,sizeof(dp));
memset(d,,sizeof(d));
for(i=;i<=n;i++)
{
for(j=;j<=m;j++)
{
if(a[i]==b[j])
{ dp[i][j]=dp[i-][j-]+;
d[i][j]=;} //标记
else
if(dp[i-][j]>dp[i][j-])
{ dp[i][j]=dp[i-][j];d[i][j]=;}
else {dp[i][j]=dp[i][j-];d[i][j]=;}
}
}
int L=dp[n][m];
i=n;
j=m;
while(L)
{
while(d[i][j]!=)
{
if(d[i][j]==)
i--;
else j--;
}
L--;
c[L]=a[i];
i--;j--;
}
cout<<c[];
for(int k=;k<dp[n][m];k++)
cout<<" "<<c[k];
cout<<endl;
}
return ;
}
最长公共子序列 LCS的更多相关文章
- 1006 最长公共子序列Lcs
1006 最长公共子序列Lcs 基准时间限制:1 秒 空间限制:131072 KB 给出两个字符串A B,求A与B的最长公共子序列(子序列不要求是连续的). 比如两个串为: abcicba abdks ...
- 动态规划之最长公共子序列LCS(Longest Common Subsequence)
一.问题描述 由于最长公共子序列LCS是一个比较经典的问题,主要是采用动态规划(DP)算法去实现,理论方面的讲述也非常详尽,本文重点是程序的实现部分,所以理论方面的解释主要看这篇博客:http://b ...
- 编程算法 - 最长公共子序列(LCS) 代码(C)
最长公共子序列(LCS) 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 给定两个字符串s,t, 求出这两个字符串最长的公共子序列的长度. 字符 ...
- C++版 - Lintcode 77-Longest Common Subsequence最长公共子序列(LCS) - 题解
版权声明:本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C++版 - L ...
- POJ 1458 Common Subsequence(最长公共子序列LCS)
POJ1458 Common Subsequence(最长公共子序列LCS) http://poj.org/problem?id=1458 题意: 给你两个字符串, 要你求出两个字符串的最长公共子序列 ...
- 51Nod 1006:最长公共子序列Lcs(打印LCS)
1006 最长公共子序列Lcs 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 给出两个字符串A B,求A与B的最长公共子序列(子序列不要求是连续的). ...
- 51nod 1006 最长公共子序列Lcs 【LCS/打印path】
1006 最长公共子序列Lcs 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 给出两个字符串A B,求A与B的最长公共子序列(子序列不要求是连续的). ...
- 每日一题-——最长公共子序列(LCS)与最长公共子串
最长公共子序列(LCS) 思路: 代码: def LCS(string1,string2): len1 = len(string1) len2 = len(string2) res = [[0 for ...
- 51nod 1006:最长公共子序列Lcs
1006 最长公共子序列Lcs 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 给出两个字符串A B,求A与B的最长公共子序列(子序列不要求是连续的). ...
- 动态规划之最长公共子序列(LCS)
转自:http://segmentfault.com/blog/exploring/ LCS 问题描述 定义: 一个数列 S,如果分别是两个或多个已知数列的子序列,且是所有符合此条件序列中最长的,则 ...
随机推荐
- EditPlus+VisualStudio配置VC简易开发环境环境
对于C++开发, 我想在Windows下大家用的最多的应该是MS的VC++.但其强大的功能背后却有着"启动速度慢","占用资源多"的缺点,尤其是VS后 ...
- JS对Array进行自定制排序
JS对Array进行自定制排序,简单的做一个记录,代码如下所示: //Test function function myFunction(){ var myArr = new Array(); var ...
- barabasilab-networkScience学习笔记3-随机网络模型
第一次接触复杂性科学是在一本叫think complexity的书上,Allen博士很好的讲述了数据结构与复杂性科学,barabasi是一个知名的复杂性网络科学家,barabasilab则是他所主导的 ...
- python web编程-web客户端编程
web应用也遵循客户服务器架构 浏览器就是一个基本的web客户端,她实现两个基本功能,一个是从web服务器下载文件,另一个是渲染文件 同浏览器具有类似功能以实现简单的web客户端的模块式urllib以 ...
- LaTex学习笔记(一)
1. 语法 命令 普通命令 环境 数据 注释 2. 物理结构 导言 指定文档类型,引入宏包,定义命令,环境等 \documentclass[options]{class} \usepackage[op ...
- sql篇 select from where group by having order by
以前,自己总是记不住如何用group by,如何用order by,什么时候用group by,什么时候用order by,什么时候两者一起用,怎么用,谁先谁后,现在,我们就一起来说一下Select ...
- 防止ViewPager和Fragment结合使用时候的数据预加载
不知道你们使用ViewPager和Fragment结合的时候发现一个问题没,如果你的每个Fragment都需要请求网络数据,并且你在请求网络数据的时候会加入进度对话框的加载显示效果,当你显示第一个Fr ...
- MySQL常用数据类型小结
在 MySQL 中,有三种主要的类型:字符串.数字和日期/时间类型. 目录 [隐藏] 1 字符串类型 2 数值类型 3 日期和时间类型 4 使用建议 5 艺搜参考 字符串类型 CHAR 0-255字 ...
- 《DSP using MATLAB》示例Example4.4
代码: x1 = [2, 3, 4]; x2 = [3, 4, 5, 6]; % x1 x2 sequences % n1 = 0:1:2; n2 = 0:1:3; n1 = 0:1:length(x ...
- javascript优化--11模式(设计模式)02
策略模式 在选择最佳策略以处理特定任务(上下文)的时候仍然保持相同的接口: //表单验证的例子 var data = { firs_name: "Super", last_name ...