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. mysql创建账号对应的数据库方法

    增加一个用户mydb密码为123450, 让他只可以在(localhost/%)%表示可以支持远程上登录,并可以对数据库mydata5_db进行查询.插入.修改.删除的操作. grant select ...

  2. nodejs 中使用shell脚本

    虽然nodejs的require('child_process').execSync可以使用shell,但其实问题特别多.尤其是符号和语法冲突,可读性也很差,只能完成一些小规模的shell使用. co ...

  3. [个人开发人员赚钱九]做一个日收入10元的APP!

    [导语]尽管讲了非常多个人开发人员的文章.但新手开发人员怎样赚自己的第一个10块钱.确是最难的事情.群里有人说都不知道干什么app赚钱.全然没有想法.而且常常问我有什么高速赚钱的方法.我仅仅能遗憾地 ...

  4. 怎么解决ORACLE 中 CHAR类型的索引问题

    在很多场景中,都有如下情况 trim(a.colunm1) = trim(b.colunm2) 应该怎么优化呢? 用到 TRIM 的很多原因是某些系统为了提高查询效率,不使用  ORACLE 的特有的 ...

  5. Spring可扩展的XML Schema机制

    可扩展的XML Schema机制 从Spring2.0开始,Spring提供了XML Schema可扩展机制,用户可以自定义XML Schema文件,并自定义XML Bean解析器,并集成到Sprin ...

  6. mysql乐观锁和悲观锁

    在多用户环境中,在同一时间可能会有多个用户更新相同的记录,这会产生冲突.这就是著名的并发性问题. 悲观锁:假定会发生并发冲突,屏蔽一切可能违反数据完整性的操作. 乐观锁:假设不会发生并发冲突,只在提交 ...

  7. FormatUtil类型格式转换

    package cn.edu.hbcf.common.utils; import java.math.BigDecimal; import java.math.BigInteger; import j ...

  8. FastDFS 常见问题

    FastDFS 常见问题 Q:/fdfs_trackerd: error while loading shared libraries: libevent-1.4.so.2: cannot open ...

  9. python zlib压缩存储到mysql列

    数据太大压缩存储,可以使用zlib中的压缩函数,代码如下: import ujson import MySQLdb import zlib import base64 kwargs = { 'host ...

  10. jfinal_BLOG v1.0

    http://git.oschina.net/fleam/jfinal_AmazeUI