[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 ...
随机推荐
- jedis连接集群
/** * 集群版 */ @Test public void JedisJiuQun() { HashSet< ...
- Ajax高级应用---Comet
非常适合处理体育比赛的分数和股票报价 1.HTTP流 将输出缓存中的内容一次性全部发送到客户端的功能是实现HTTP流的关键所在.
- EF 表联合查询 join
有两张表m_Dept.m_User,联合查询 linq方式.EF方式 private void Add() { List<m_Dept> lst = new List<m_Dept& ...
- [svc][db]centos7 Mariadb安装
yum install mariadb-server mariadb -y 修改配置: [mysqld] default-storage-engine = innodb innodb_file_per ...
- vue的单文件组件
五. 单文件组件 1. .vue文件 .vue文件,称为单文件组件,是Vue.js自定义的一种文件格式,一个.vue文件就是一个单独的组件,在文件内封装了组件相关的代码:html.css.js .vu ...
- RIP协议两个版本对不连续子网的支持情况实验
一.连续子网与不连续子网 我们经常见到说RIPv1不支持不连续子网,仅支持连续子网,那么什么是连续子网,什么是不连续子网呢? l 不连续子网:指在一个网络中,某几个连续由同一主网划分的子网在中间被多 ...
- Github上fork项目后与原项目保持同步
**步骤** 假设来源为 `https://github.com/_original/_project.git` fork 项目为 `https://github.com/_your/_projec ...
- UITextField小结
//初始化textfield并设置位置及大小 UITextField *text = [[UITextField alloc]initWithFrame:CGRectMake(20, 20, 130, ...
- Linux多条指令之间;和&&
Linux 中经常使用到一个命令,如 make && make install,这里也可以使用 make ; make install,那么在 Linux 中执行命令 ; 和 & ...
- spring cloud 启动报错问题 Input length = 1
Caused by: org.yaml.snakeyaml.error.YAMLException: java.nio.charset.MalformedInputException: Input l ...