-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
lcs.cpp
85 lines (68 loc) · 1.79 KB
/
lcs.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
/**
* Given two strings X(1..m) and Y(1..n). Find the longest common substring
* that appears left to right (but not necessarily in a contiguous manner)
* in both strings. For example X = "ABCBDAB" and Y = "BDCABA"
* LCS(X,Y) = {"BCBA", "BDAB", "BCAB"}.
* LCS-LENGTH(X,Y) = 4
*/
#include <iostream>
#include <string>
#include <vector>
size_t max( size_t a, size_t b ) {
return a > b ? a : b;
}
size_t max(size_t a, size_t b, size_t c) {
return (max(a,b) > c ) ? max(a,b) : c ;
}
void longest_common_subsequence(std::vector<std::vector<size_t>> & lcs,
std::string s1, std::string s2)
{
size_t m = s1.length();
size_t n = s2.length();
for ( size_t j = 0; j <= n; ++j ) {
lcs[0][j] = 0;
}
for ( size_t i = 0; i <= m; ++i ) {
lcs[i][0] = 0;
}
for( size_t i = 0; i < m; ++i ) {
for( size_t j = 0; j < n; ++j ) {
lcs[i+1][j+1] = lcs[i][j]; //getting previous max val
if (s1[i] == s2[j]) {
lcs[i+1][j+1]++;
} else {
lcs[i+1][j+1] = max(lcs[i][j+1], lcs[i+1][j]);
}
}
}
std::cout << "Longest common subsequence length - " << lcs[m][n] << std::endl;
size_t i = m;
size_t j = n;
size_t index = lcs[m][n];
std::string seq(lcs[m][n], ' ');
while( i > 0 && j > 0 ) {
if ( s1[i-1] == s2[j-1] ) {
seq[index -1 ] = s1[i-1];
--index;
--i;
--j;
} else if ( lcs[i-1][j] > lcs[i][j-1] ) {
--i;
} else {
--j;
}
}
std::cout << "One of possible many Longest Common Subsequence is : " << seq << std::endl;
}
int main()
{
std::string str1, str2;
std::cout << "Longest common subsequence Problem:\n";
std::cout << "Enter string 1:";
std::cin >> str1;
std::cout << "Enter string 2:";
std::cin >> str2;
std::vector<std::vector<size_t>> lcs( str1.length() + 1, std::vector<size_t>(str2.length() + 1));
longest_common_subsequence(lcs, str1, str2);
return 0;
}