最长公共子序列 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,如果分别是两个或多个已知数列的子序列,且是所有符合此条件序列中最长的,则 ...
随机推荐
- QQ的账号登录及api操作
.qq.php <?php /** * PHP Library for qq.com * * @author */ class qqPHP { function __construct($app ...
- PHP json数据格式化方法
php 的json_encode能把数组转换为json格式的字符串.字符串没有缩进,中文会转为unicode编码,例如\u975a\u4ed4.人阅读比较困难.现在这个方法在json_encode的基 ...
- [Linux] 取得服务器版本
1) 登录到服务器执行 lsb_release -a ,即可列出所有版本信息,例如: [root@3.5.5Biz-46 ~]# lsb_release -a LSB Version: 1.3 Dis ...
- 深入理解ASP.NET 5的依赖注入
(此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:ASP.NET 5整个底层都架构于依赖注入机制之下,今天介绍的文章详细介绍了内置依赖注 ...
- 信号通讯编程,王明学learn
信号通讯编程 在Linux系统中,信号(signal)同样也是最为古老的进程间通信机制. 一.信号类型 Linux系统支持的所有信号均定义在/usr/include/asm/signal.h(展示), ...
- Emacs 之窗口管理
// */ // ]]> Emacs 之窗口管理 Table of Contents 1. Emacs 窗口相关 1.1. Emacs 里调整 window 大小 1.2. Emacs winn ...
- Struts2文件上传下载
Struts2文件上传 Struts2提供 FileUpload拦截器,用于解析 multipart/form-data 编码格式请求,解析上传文件的内容,fileUpload拦截器 默认在defau ...
- 浏览器 - Firefox开发者附加组件
Firefox开发者版本下载地址: https://www.mozilla.org/zh-CN/firefox/channel/desktop/#developer 教程: https://devel ...
- UpdatePanel的使用方法
UpdatePanel控件也是Ajax里用得最多的控件之一,UpdatePanel控件是用来局部更新网页上的内容,网页上要局部更新的内容必须放在UpdatePanel控件里,他必须和上一次说的Scri ...
- POJ 2002 统计正方形 HASH
题目链接:http://poj.org/problem?id=2002 题意:给定n个点,问有多少种方法可以组成正方形. 思路:我们可以根据两个点求出对应正方形[有2个一个在两点左边,一个在两点右边] ...