This is the classic LCS problem. Since it requires you to print one longest common subsequence, just use the O(m*n)-space version here.

My accepted code is as follows.

 #include <iostream>
#include <vector>
#include <algorithm> using namespace std; vector<int> lcs(vector<int>& a, vector<int>& b) {
int n = a.size(), m = b.size();
vector<vector<int> > dp(n + , vector<int> (m + , ));
for (int i = ;i <= n; i++) {
for (int j = ; j <= m; j++) {
if (a[i - ] == b[j - ]) dp[i][j] = dp[i - ][j - ] + ;
else dp[i][j] = max(dp[i][j - ], dp[i - ][j]);
}
}
vector<int> res;
for (int i = n, j = m; i >= && j >= ;) {
if (a[i - ] == b[j - ]) {
res.push_back(a[i - ]);
i--;
j--;
}
else if (dp[i - ][j] >= dp[i][j - ]) i--;
else j--;
}
reverse(res.begin(), res.end());
return res;
} int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int n, m;
while (scanf("%d %d", &n, &m) != EOF) {
vector<int> a(n);
vector<int> b(m);
for (int i = ; i < n; i++)
scanf("%d", &a[i]);
for (int i = ; i < m; i++)
scanf("%d", &b[i]);
vector<int> res = lcs(a, b);
for (int i = ; i < (int)res.size(); i++)
printf("%d ", res[i]);
printf("\n");
}
return ;
}

Well, try this problem hereand get Accepted :)

[HackerRank] The Longest Common Subsequence的更多相关文章

  1. 动态规划求最长公共子序列(Longest Common Subsequence, LCS)

    1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...

  2. LintCode Longest Common Subsequence

    原题链接在这里:http://www.lintcode.com/en/problem/longest-common-subsequence/ 题目: Given two strings, find t ...

  3. [UCSD白板题] Longest Common Subsequence of Three Sequences

    Problem Introduction In this problem, your goal is to compute the length of a longest common subsequ ...

  4. LCS(Longest Common Subsequence 最长公共子序列)

    最长公共子序列 英文缩写为LCS(Longest Common Subsequence).其定义是,一个序列 S ,如果分别是两个或多个已知序列的子序列,且是所有符合此条件序列中最长的,则 S 称为已 ...

  5. Longest Common Subsequence

    Given two strings, find the longest common subsequence (LCS). Your code should return the length of  ...

  6. Longest Common Subsequence & Substring & prefix

    Given two strings, find the longest common subsequence (LCS). Your code should return the length of  ...

  7. Dynamic Programming | Set 4 (Longest Common Subsequence)

    首先来看什么是最长公共子序列:给定两个序列,找到两个序列中均存在的最长公共子序列的长度.子序列需要以相关的顺序呈现,但不必连续.例如,"abc", "abg", ...

  8. Lintcode:Longest Common Subsequence 解题报告

    Longest Common Subsequence 原题链接:http://lintcode.com/zh-cn/problem/longest-common-subsequence/ Given ...

  9. UVA 10405 Longest Common Subsequence (dp + LCS)

    Problem C: Longest Common Subsequence Sequence 1: Sequence 2: Given two sequences of characters, pri ...

随机推荐

  1. 修改easyui panel 默认样式

    有这么个需求需要修改easyui panel头部中的背景色.于是根据panel中的最终被浏览器解析出来的类名,直接修改这个css样式,设置backgroud-color这个属性,发现不管用. 于是,就 ...

  2. Linux上寻找并杀死僵尸进程

    转载: http://blog.csdn.net/shanzhizi/article/details/47320595 linux服务器上,多少会出现一些僵尸进程,下面介绍如何快速寻找和消灭这些僵尸进 ...

  3. [Android进阶]Binder学习(初始篇)

    Android中Binder学习(初始篇) 本篇博客学习自侯亮的博客.地址为: 红茶一杯话Binder 1 什么是Binder? 简单地说.Binder是Android平台上的一种跨进程交互技术. 该 ...

  4. Rabbitmq消息队列(三) 工作队列

    1.简介 默认来说,RabbitMQ会按顺序得把消息发送给每个消费者(consumer).平均每个消费者都会收到同等数量得消息.这种发送消息得方式叫做——轮询(round-robin). 工作队列(又 ...

  5. memcache概念浅谈及名称混乱之区分

    关于memcache这个现在应用广泛的组件,大大提高的网站的响应速度,也方便了程序开发缓存的应用.但是目前针对memcache,网上的资料 大同小异,尤其基于LAMP的网站居多,php/pcel又有两 ...

  6. 点滴积累【SQL Server】---使用Kettle实时同步DB2数据到SQLserver

    效果: 描述: 此操作适用于单点登录的同步用户. 首先,使用kettle将DB2数据同步到SQL中,然后添加到windows的任务计划中.定时执行同步数据. 特殊说明:此工具涉及到公司版权,所以不方便 ...

  7. 设置phpcms v9黄页模块作为首页方法

    如果我们根据需要,想把黄页作为单独的网站,我们可以用模块化安装,并且首页设置,那么仿站网就说说详细的步骤.首先,我们需要安装最新版本的phpcms V9其次,下载黄页模块,然后进行根目录的替换.再次, ...

  8. Xcode5下使用纯代码构建简单的HelloWorld程序

    转自:http://blog.csdn.net/developerxyf/article/details/12874935 新发布的Xcode5在使用模板创建工程的时候取消了以往是否要选择storyb ...

  9. JS事件类型详解

    一般事件 onclick IE3.N2 鼠标点击时触发 此事件 ondblclick IE4.N4 鼠标双击时触发 此事件 onmousedown IE4.N4 按下鼠标时触发 此事件 onmouse ...

  10. Ubuntu下键盘输入错乱问题,输入双引号输出的是@符号,输入#号输出的是未知语言的字符

    装完搜狗后,键盘开始出现混乱,切换到英文输入法,输入双引号输出的是@符号,输入#号输出的是未知语言的字符. 网上有的说在 system - keyboard - Input Source 下看看是否是 ...