-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLongestCommonSubsequence.cpp
165 lines (140 loc) · 4.26 KB
/
LongestCommonSubsequence.cpp
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Techie delight https://www.techiedelight.com/longest-common-subsequence/
#include <string>
#include <iostream>
#include <unordered_map>
#include <vector>
const int maxM = 100;
const int maxN = 100;
std::string getLCSString(const std::string X, const std::string Y, int m, int n, int lookup[][maxN])
{
if (m == 0 || n == 0) {
return "";
}
if (X[m - 1] == Y[n - 1]) {
return getLCSString(X, Y, m - 1, n - 1, lookup) + X[m - 1];
}
if (lookup[m - 1][n] > lookup[m][n - 1]) {
return getLCSString(X, Y, m - 1, n, lookup);
} else {
return getLCSString(X, Y, m, n - 1, lookup);
}
}
std::string getLCS(const std::string X, const std::string Y)
{
int m = X.size();
int n = Y.size();
int lookup[maxM][maxN];
for (int i = 0; i <= m; ++i) {
for (int j = 0; j <= n; ++j) {
if (i == 0 || j == 0) {
lookup[i][j] = 0;
} else if (X[i - 1] == Y[j - 1]) {
lookup[i][j] = 1 + lookup[i - 1][j - 1];
} else {
lookup[i][j] = std::max(lookup[i][j - 1], lookup[i - 1][j]);
}
}
}
std::string lcs = getLCSString(X, Y, m, n, lookup);
return lcs;
}
int spaceOptimizedHelp(const std::string X, const std::string Y)
{
int m = X.size();
int n = Y.size();
int lookup[n + 1];
for (int i = 0; i <= m; ++i) {
int previous = lookup[0];
for (int j = 0; j <= n; ++j) {
int backup = lookup[j];
if (i == 0 || j == 0) {
lookup[j] = 0;
} else if (X[i - 1] == Y[j - 1]) {
lookup[j] = 1 + previous;
} else {
lookup[j] = std::max(lookup[j], lookup[j - 1]);
}
previous = backup;
}
}
return lookup[n];
}
int spaceOptimized(const std::string X, const std::string Y)
{
if (X.size() < Y.size()) {
return spaceOptimizedHelp(X , Y);
} else {
return spaceOptimizedHelp(Y, X);
}
}
int botomUp(const std::string X, const std::string Y)
{
int m = X.size();
int n = Y.size();
int lookup[m + 1][n + 1];
for (int i = 0; i <= m; ++i) {
for (int j = 0; j <= n; ++j) {
if (i == 0 || j == 0) {
lookup[i][j] = 0;
} else if (X[i - 1] == Y[j - 1]) {
lookup[i][j] = 1 + lookup[i - 1][j - 1];
} else {
lookup[i][j] = std::max(lookup[i][j - 1], lookup[i - 1][j]);
}
}
}
return lookup[m][n];
}
int memoization(const std::string X, const std::string Y, int m, int n,
std::unordered_map<std::string, int>& map)
{
if (m == 0 || n == 0) {
return 0;
}
std::string key = m + " | " + n;
if (map.find(key) == map.end()) {
if (X[m - 1] == Y[n - 1]) {
map[key] = 1 + memoization(X, Y, m - 1, n - 1, map);
} else {
map[key] = std::max(memoization(X, Y, m - 1, n, map),
memoization(X, Y, m , n - 1, map));
}
}
return map[key];
}
int memoization(const std::string X, const std::string Y)
{
std::unordered_map<std::string, int> map;
return memoization(X, Y, X.size(), Y.size(), map);
}
int kniveRecursion(const std::string X, const std::string Y, int m, int n)
{
if (m == 0 || n == 0) {
return 0;
}
if (X[m - 1] == Y[n - 1]) {
return 1 + kniveRecursion(X, Y, m - 1, n - 1);
}
return std::max(kniveRecursion(X, Y, m - 1, n), kniveRecursion(X, Y, m, n - 1));
}
int kniveRecursion(const std::string X, const std::string Y)
{
return kniveRecursion(X, Y, X.size(), Y.size());
}
void test(const std::string X, const std::string Y)
{
std::cout << "X : " << X <<"\nY : " << Y;
std::cout << "\nLength of LCS using knive recursion : " << kniveRecursion(X, Y);
std::cout << "\nLength of LCS using using memozation : " << memoization(X, Y);
std::cout << "\nLength of LCS using using bottom up : " << botomUp(X, Y);
std::cout << "\nLength of LCS using space optimized : " << spaceOptimized(X, Y);
std::cout << "\nLCS string : " << getLCS(X, Y);
std::cout << "\n=======================================\n";
}
int main()
{
std::string X = "ABCBDAB";
std::string Y = "BDCABA";
test(X, Y);
return 0;
}