[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.

 1 #include <iostream>
 2 #include <vector>
 3 #include <algorithm>
 4
 5 using namespace std;
 6
 7 vector<int> lcs(vector<int>& a, vector<int>& b) {
 8     int n = a.size(), m = b.size();
 9     vector<vector<int> > dp(n + 1, vector<int> (m + 1, 0));
10     for (int i = 1 ;i <= n; i++) {
11         for (int j = 1; j <= m; j++) {
12             if (a[i - 1] == b[j - 1]) dp[i][j] = dp[i - 1][j - 1] + 1;
13             else dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]);
14         }
15     }
16     vector<int> res;
17     for (int i = n, j = m; i >= 1 && j >= 1;) {
18         if (a[i - 1] == b[j - 1]) {
19             res.push_back(a[i - 1]);
20             i--;
21             j--;
22         }
23         else if (dp[i - 1][j] >= dp[i][j - 1]) i--;
24         else j--;
25     }
26     reverse(res.begin(), res.end());
27     return res;
28 }
29
30 int main() {
31     /* Enter your code here. Read input from STDIN. Print output to STDOUT */
32     int n, m;
33     while (scanf("%d %d", &n, &m) != EOF) {
34         vector<int> a(n);
35         vector<int> b(m);
36         for (int i = 0; i < n; i++)
37             scanf("%d", &a[i]);
38         for (int i = 0; i < m; i++)
39             scanf("%d", &b[i]);
40         vector<int> res = lcs(a, b);
41         for (int i = 0; i < (int)res.size(); i++)
42             printf("%d ", res[i]);
43         printf("\n");
44     }
45     return 0;
46 }

Well, try this problem hereand get Accepted :)

时间: 2024-10-14 07:53:24

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

Longest Common Subsequence

Problem statement: Given two strings, find the longest common subsequence (LCS). Your code should return the length of LCS. Have you met this question in a real interview? Yes Clarification What's the definition of Longest Common Subsequence? https:/

Dynamic Programming | Set 4 (Longest Common Subsequence)

首先来看什么是最长公共子序列:给定两个序列,找到两个序列中均存在的最长公共子序列的长度.子序列需要以相关的顺序呈现,但不必连续.例如,"abc", "abg", "bdf", "aeg", '"acefg"等都是"abcdefg"的子序列.因此,一个长度为n的序列拥有2^n中可能的子序列(序列中的每一个元素只有选或者不选两种可能,因此是2^n). Example: LCS for inp

uva10405 - Longest Common Subsequence(LIS,最长共同自序列)

题目:uva10405 - Longest Common Subsequence(LIS,最长共同自序列) 题目大意:找出两个字符串中的最长公共的子序列. 解题思路:这类问题是第一次接触,不知道怎么做.百度了一下,发现了递推公式:dp[i][j]:代表第一个字符串的前i个字符和第二个字符串的前j个字符比较能得到的最长的公共子序列.s[i] == s[j] ,dp[i][j] = dp[i - 1][j - 1] + 1: s[i] != s[j] , dp[i][j] = Max (dp[i][

[Algorithms] Longest Common Subsequence

The Longest Common Subsequence (LCS) problem is as follows: Given two sequences s and t, find the length of the longest sequence r, which is a subsequence of both s and t. Do you know the difference between substring and subequence? Well, substring i

HDU 2253 Longest Common Subsequence Again

其实这个题我还不会,学长给了一个代码交上去过了,据说用到了一种叫做位压缩的技术,先贴代码吧,以后看懂了再来写 #include <stdio.h> #include <string.h> #define M 30005 #define SIZE 128 #define WORDMAX 3200 #define BIT 32 char s1[M], s2[M]; int nword; unsigned int str[SIZE][WORDMAX]; unsigned int tmp1

uva 10405 Longest Common Subsequence (最长公共子序列)

uva 10405 Longest Common Subsequence Sequence 1: Sequence 2: Given two sequences of characters, print the length of the longest common subsequence of both sequences. For example, the longest common subsequence of the following two sequences: abcdgh a

UVA 10405 Longest Common Subsequence

最长公共子系列,简单的dp,不过注意有空格,所以,在读字符串的时候,尽量用gets读,这样基本没问题 #include<iostream> #include<cstdio> #include<string> #include<cstring> using namespace std; int dp[1001][1001]; int MAX(int x,int y) { if (x>y) return x; else return y; } int ma

Longest Common Subsequence (DP)

Given two strings, find the longest common subsequence (LCS). Your code should return the length of LCS. Example For "ABCD" and "EDCA", the LCS is "A" (or "D", "C"), return 1. For "ABCD" and &quo

2017-5-14 湘潭市赛 Longest Common Subsequence 想法题

Longest Common Subsequence Accepted : 7 Submit : 66 Time Limit : 3000 MS Memory Limit : 65536 KB Longest Common Subsequence Bobo has a sequence A=(a1,a2,-,an) of length n. He would like to know f(0),f(1),f(2) and f(3) where f(k) denotes the number of