[HackerRank] The Longest Common Subsequence
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的更多相关文章
- 动态规划求最长公共子序列(Longest Common Subsequence, LCS)
1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...
- LintCode Longest Common Subsequence
原题链接在这里:http://www.lintcode.com/en/problem/longest-common-subsequence/ 题目: Given two strings, find t ...
- [UCSD白板题] Longest Common Subsequence of Three Sequences
Problem Introduction In this problem, your goal is to compute the length of a longest common subsequ ...
- LCS(Longest Common Subsequence 最长公共子序列)
最长公共子序列 英文缩写为LCS(Longest Common Subsequence).其定义是,一个序列 S ,如果分别是两个或多个已知序列的子序列,且是所有符合此条件序列中最长的,则 S 称为已 ...
- Longest Common Subsequence
Given two strings, find the longest common subsequence (LCS). Your code should return the length of ...
- Longest Common Subsequence & Substring & prefix
Given two strings, find the longest common subsequence (LCS). Your code should return the length of ...
- Dynamic Programming | Set 4 (Longest Common Subsequence)
首先来看什么是最长公共子序列:给定两个序列,找到两个序列中均存在的最长公共子序列的长度.子序列需要以相关的顺序呈现,但不必连续.例如,"abc", "abg", ...
- Lintcode:Longest Common Subsequence 解题报告
Longest Common Subsequence 原题链接:http://lintcode.com/zh-cn/problem/longest-common-subsequence/ Given ...
- UVA 10405 Longest Common Subsequence (dp + LCS)
Problem C: Longest Common Subsequence Sequence 1: Sequence 2: Given two sequences of characters, pri ...
随机推荐
- 产品经理PM
首先希望大家记住的就是,千万不要以为产品经理是什么高大上的光环,产品经理其实只是一种状态,一种心态而已. 大家可能看到BAT每年都会从校园里面招聘一些产品经理,尤其是我们腾讯,声称以产品为王,每年投产 ...
- js限制文本框只能输入数字方法
输入大于0的正整数 <input onkeyup="if(this.value.length==1){this.value=this.value.replace(/[^1-9]/g,' ...
- apache设置头
Apache 及开启压缩及Header信息隐藏:http://centilinux.blog.51cto.com/1454781/792820
- uva 10808 - Rational Resistors(基尔霍夫定律+高斯消元)
题目链接:uva 10808 - Rational Resistors 题目大意:给出一个博阿含n个节点,m条导线的电阻网络,求节点a和b之间的等效电阻. 解题思路:基尔霍夫定律,不论什么一点的电流向 ...
- 绝不能错过的10款最新OpenStack网络运维 & 监控工具
摘要 今天我们要推荐给大家的是关于奥斯汀OpenStack Summit的OpenStack网络方面功能与工具相关的技术演讲. 希望可以帮助国内的开发者.架构师和用户更好地了解OpenStack在SD ...
- thread_为什么多线程是个坏主意
转 http://ifeve.com/why-threads-bad/ 在 Unix编程艺术 中,提到了尽量避免多线程编程模型, 认为这样只会增加复杂度, 提倡使用多进程, 这样本质上就可以避免多线程 ...
- 608. Two Sum - Input array is sorted【medium】
Given an array of integers that is already sorted in ascending order, find two numbers such that the ...
- SockIOPool
1. SockIOPool – SockIO池化管理,为上层提供的接口是实例化函数[主要是指定memcached服务器地址,各个机器的权重]:根据key&hashCode获取SockIO-网络 ...
- POSTGRESQL 9.1 FATAL: password authentication failed for user "postgres"
1.配置postgreql 可以远程访问: sudo vim /etc/postgresql/9.1/main/postgresql.conf root@ubuntuserver:~# sudo vi ...
- linux上FTP服务器搭建
一.启动vsftpd服务 1. 启动VSFTP服务器 A. cenos下运行:yum install vsftpd B. 登录Linux主机后,运行命令:"service vsftpd st ...