-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongestCommonSubsequence.cpp
More file actions
234 lines (198 loc) · 5 KB
/
LongestCommonSubsequence.cpp
File metadata and controls
234 lines (198 loc) · 5 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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <unordered_map>
class Subsequence
{
private:
std::vector<std::string> subseqs;
const std::string& input;
public:
Subsequence(const std::string& str)
: input(str)
{
}
void generate()
{
std::string subseq;
generateHelper(input, 0, subseq);
}
void print()
{
std::cout << "Subsequences of " << input << " are:" << std::endl;
for (const auto& iter : subseqs)
{
std::cout << iter << std::endl;
}
}
const std::vector<std::string> get() const
{
return subseqs;
}
private:
void generateHelper(const std::string& input, int indx, std::string subseq)
{
subseqs.push_back(subseq);
for (int i = indx; i < input.length(); ++i)
{
subseq.append(1, input[i]);
generateHelper(input, i + 1, subseq);
subseq.pop_back();
}
}
};
class BruteForceLongestCommonSubsequence
{
private:
const std::string& str1;
const std::string& str2;
public:
BruteForceLongestCommonSubsequence(const std::string& input1, const std::string& input2)
: str1(input1), str2(input2)
{
}
void find()
{
Subsequence obj1(str1);
Subsequence obj2(str2);
obj1.generate();
obj2.generate();
std::vector<std::string> common_sub;
int longest_length = 0;
std::vector<std::string> subseqs1 = obj1.get();
std::vector<std::string> subseqs2 = obj2.get();
for (int i = 0; i < subseqs1.size(); ++i) {
for (int j = 0; j < subseqs2.size(); ++j) {
if (subseqs1[i].compare(subseqs2[j]) == 0) {
common_sub.push_back(subseqs1[i]);
longest_length = std::max(static_cast<size_t>(longest_length), subseqs1[i].length());
}
}
}
int sz = common_sub.size();
int i = 0;
while (i < sz)
{
if (common_sub[i].length() < longest_length)
{
common_sub.erase(common_sub.begin() + i);
sz = common_sub.size();
} else {
i++;
}
}
std::cout << "Brute force approach: Longest common subsequences of " << str1 << " and " << str2 << " are: " << std::endl;
for (const auto& iter : common_sub)
{
std::cout << iter << std::endl;
}
}
};
class DynamicProgramming
{
private:
const std::string& str1;
const std::string& str2;
std::unordered_map<std::string, int> map;
std::vector<std::vector<int>> mat;
public:
DynamicProgramming(const std::string& input1, const std::string& input2)
: str1(input1), str2(input2)
{
}
void topDown()
{
int len = topDownHelper(str1.length() - 1, str2.length() - 1);
std::cout << "DP: TOP DOWN: Length of longest common subsequence of " << str1 << " and " << str2 << " is " << len << std::endl;
}
void bottomUp()
{
int len = bottomUpHelper();
std::cout << "DP: BOTTOM UP: Length of longest common subsequence of " << str1 << " and " << str2 << " is " << len << std::endl;
}
void allLCS()
{
std::vector<std::string> res = getLCSTopDown(mat.size() - 1, mat[0].size() - 1);
std::cout << "All the LCS are" << std::endl;
for (auto iter : res)
{
std::cout << iter << std::endl;
}
}
private:
std::vector<std::string> getLCSTopDown(int indx1, int indx2)
{
if (indx1 <= 0 || indx2 <= 0)
{
std::vector<std::string> v;
v.push_back("");
return v;
}
if (str1[indx1 - 1] == str2[indx2 - 1])
{
std::vector<std::string> result = getLCSTopDown(indx1 - 1, indx2 - 1);
for (std::string & iter : result)
iter.push_back(str1[indx1 - 1]);
return result;
} else if (mat[indx1 - 1][indx2] > mat[indx1][indx2 - 1]){
// Move in the direction of greater length
return getLCSTopDown(indx1 - 1, indx2);
} else if (mat[indx1][indx2 - 1] > mat[indx1 - 1][indx2]) {
// Move in the direction of greater length
return getLCSTopDown(indx1, indx2 - 1);
} else {
// Both directions can result in max value. So, try both
std::vector<std::string> left = getLCSTopDown(indx1 - 1, indx2);
std::vector<std::string> top = getLCSTopDown(indx1, indx2 - 1);
//Merge results
top.insert(top.end(), left.begin(), left.end());
return top;
}
}
int topDownHelper(int indx1, int indx2)
{
if (indx1 < 0 || indx2 < 0)
return 0;
std::string key = std::to_string(indx1) + "_" + std::to_string(indx2);
if (map.find(key) == map.end())
{
if (str1[indx1] == str2[indx2])
{
map[key] = 1 + topDownHelper(indx1 - 1, indx2 - 1);
} else {
map[key] = std::max(topDownHelper(indx1, indx2 - 1), topDownHelper(indx1 - 1, indx2));
}
}
return map[key];
}
int bottomUpHelper()
{
mat = std::vector<std::vector<int>>(str1.length() + 1, std::vector<int>(str2.length() + 1, 0));
for (int i = 1; i <= str1.length(); ++i)
{
for (int j = 1; j <= str2.length(); ++j)
{
if (str1[i - 1] == str2[j - 1])
{
mat[i][j] = mat[i - 1][j - 1] + 1;
} else {
mat[i][j] = std::max(mat[i][j - 1], mat[i - 1][j]);
}
}
}
return mat[str1.length()][str2.length()];
}
};
int main()
{
std::string str1("ABCBDAB");
std::string str2("BDCABA");
BruteForceLongestCommonSubsequence obj1(str1, str2);
obj1.find();
DynamicProgramming obj2(str1, str2);
obj2.topDown();
obj2.bottomUp();
obj2.allLCS();
return 0;
}