-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathLongest_common_sequence.cpp
More file actions
103 lines (71 loc) · 2.21 KB
/
Longest_common_sequence.cpp
File metadata and controls
103 lines (71 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
Given two strings s1 and s2, return the length of their longest common subsequence.
If there is no common subsequence, return 0.
A subsequence of a string is a new string generated from the original string with some characters
(can be none) deleted without changing the relative order of the remaining characters.
For example, "ace" is a subsequence of "abcde".
A common subsequence of two strings is a subsequence that is common to both strings.
Example :
Input : s1 = "ABCDGH", s2 = "AEDFHR"
Output: 3
Explanation : "ADH" is the LCS between s1 and s2
Input : s1 = "AGGTAB", s2 = "GXTXAYB"
Output: 4
Explanation : “GTAB” is the LCS between s1 and s2
Input : s1 = "ABCHG", s2 = "ABGCF"
Output: 3
Explanation : "ABC" & "ABG" is the LCS between s1 and s2
*/
#include<iostream>
#include<vector>
using namespace std;
int LCS(string& s1, string& s2, int i, int j) {
if(i == s1.size() || j == s2.size()) {
// either of the sequences become empty
return 0;
}
if(s1[i] == s2[j]) {
int l3 = LCS(s1, s2, i+1, j+1);
return 1 + l3;
}
int l1 = LCS(s1, s2, i+1, j);
int l2 = LCS(s1, s2, i, j+1);
return max(l1, l2);
}
int LCSTopDown(string& s1, string& s2, int i, int j, vector<vector<int>>& dp) {
if(i == s1.size() || j == s2.size()) {
// either of the sequences become empty
return dp[i][j] = 0;
}
// lookup
if(dp[i][j] != -1) return dp[i][j];
if(s1[i] == s2[j]) {
int l3 = LCSTopDown(s1, s2, i+1, j+1, dp);
return dp[i][j] = 1 + l3;
}
int l1 = LCSTopDown(s1, s2, i+1, j, dp);
int l2 = LCSTopDown(s1, s2, i, j+1, dp);
return dp[i][j] = max(l1, l2);
}
int LCSBottomUp(string s1, string s2, int m, int n) {
// automatically handle the base case during initialisation
vector<vector<int>> dp(m+1, vector<int>(n+1, 0));
for(int i=m-1; i>=0; i--) {
for(int j=n-1; j>=0; j--) {
if(s1[i] == s2[j]) dp[i][j] = 1 + dp[i+1][j+1];
else dp[i][j] = max(dp[i+1][j], dp[i][j+1]);
}
}
return dp[0][0];
}
int main() {
string s1("ABCDGH");
string s2("AEDFHR");
int m = s1.size();
int n = s2.size();
cout << LCS(s1, s2, 0, 0) << endl;
vector<vector<int>> dp(m+1, vector<int>(n+1, -1));
cout << LCSTopDown(s1, s2, 0, 0, dp) << endl;
cout << LCSBottomUp(s1, s2, m, n) << endl;
return 0;
}