-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLCS.cpp
More file actions
54 lines (45 loc) · 1.33 KB
/
LCS.cpp
File metadata and controls
54 lines (45 loc) · 1.33 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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
string lcs(string s1, string s2) {
int m = s1.size();
int n = s2.size();
// Create a 2D vector to store the lengths of LCS for substrings
vector<vector<int> > dp(m + 1, vector<int>(n + 1, 0));
// Fill in the dp table
for (int i = 1; i <= m; ++i) {
for (int j = 1; j <= n; ++j) {
if (s1[i - 1] == s2[j - 1]) {
dp[i][j] = dp[i - 1][j - 1] + 1; // Match found, increment length
} else {
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); // No match, take maximum length
}
}
}
// Reconstruct the LCS from dp table
string lcs_string = "";
int i = m, j = n;
while (i > 0 && j > 0) {
if (s1[i - 1] == s2[j - 1]) {
lcs_string = s1[i - 1] + lcs_string;
--i;
--j;
} else if (dp[i - 1][j] > dp[i][j - 1]) {
--i;
} else {
--j;
}
}
return lcs_string;
}
int main() {
string s1, s2;
cout << "Enter the first string: ";
cin >> s1;
cout << "Enter the second string: ";
cin >> s2;
string result = lcs(s1, s2);
cout << "Longest Common Subsequence (LCS): " << result << endl;
return 0;
}