-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathLcs-basic.cpp
58 lines (50 loc) · 1.05 KB
/
Lcs-basic.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
/**
* @file Lcs-basic.cpp
* @author prakash ([email protected])
* @brief
* @version 0.1
* @date 2021-08-28
*
* @copyright Copyright (c) 2021
*
*/
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int main() {
string s1 = "Hellollworld", s2 = "Hello";
int M = s1.length();
int N = s2.length();
int m[M + 1][N + 1];
for (int i = 0; i <= M; i++) {
for (int j = 0; j <= N; j++) {
if (i == 0 || j == 0) {
m[i][j] = 0;
continue;
}
if (s1[i] == s2[j])
m[i][j] = 1 + m[i - 1][j - 1];
else
m[i][j] = max(m[i][j - 1], m[i - 1][j]);
}
}
string str;
int i = M, j = N;
while (i > 0 && j > 0) {
if (s1[i - 1] == s2[j - 1]) {
str.push_back(s1[i - 1]);
i--;
j--;
}
else if (m[i][j - 1] < m[i - 1][j]) {
str.push_back(s2[j - 1]);
j--;
} else {
i--;
}
}
reverse(str.begin(), str.end());
std::cout << "Longest common subsequence : " << str << " " << m[M][N]
<< std::endl;
}