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. jBoss无法通过IP地址访问,只能用localhost\127.0.0.1访问

    http://feng88724.iteye.com/blog/263211 JBOSS版本:4.2.2GA 症状:服务器无法通过IP地址去访问,只能用127.0.0.1或者localhost来访问. ...

  2. Actors编程模型

    Actors模型(Actor model)首先是由Carl Hewitt在1973定义, 由Erlang OTP (Open Telecom Platform) 推广,其 消息传递更加符合面向对象的原 ...

  3. mongodb - mongotop

    mongodb - mongotop # ./mongotop 2016-03-12T16:37:32.141+0800 connected to: 127.0.0.1 ns total read w ...

  4. 2012全球SEO行业调查报告

    这份报告是SEOmoz对每两年一度举办的SEO行业调查进行的分析数据,上次调查是在2010年.该调查,主要围绕SEO从业人员的特征.工作内容时间分配比例.SEO相关消费和预算.对未来市场的看法.seo ...

  5. php面象对象魔术方法的使用

    class Moshu{ public $number; public function __construct($str)//构造方法,new实例时,自动调用 { //require('/confi ...

  6. laydate 和 Vue 奇怪的清空问题

    laydate的input,会自动被清空,当别的input修改的时候.改成这样既可解决 <td><input type="text" id="retur ...

  7. JS prototype 属性

    String.prototype.trim=function(){ return this.replace(/(^\s*)|(\s*$)/g, "");}

  8. Windows+Nginx+IIS做图片分布式存储详细步骤

    最近几天,一直在学习nginx在windows平台下的使用,为了寻找几种大量图片分布式存储而且有相对简单的存储方案 nginx是一种,还找到一种MongoDB GridFS 这两种方案我还是比较中意的 ...

  9. hdu4711Weather 概率dp

    //第i个城市到第j个城市的概率ma[i][j] //第i天的天气天气wet[i] //第i个城市天气为j的概率wet_m[i][j] //Hovey从0点開始,找出其概率最大的路线 //dp[i][ ...

  10. 龙芯CAN测试(sja1000)

    测试方案 CAN0和CAN1相连,互相收发数据.连接方式如下图: 使用扩展模式CAN1发送数据CAN0接收数据. 使用标准模式CAN1发送数据CAN0接收数据. 使用EJTAG中bin文件夹内的can ...