-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLCS (1).cpp
More file actions
40 lines (30 loc) · 937 Bytes
/
LCS (1).cpp
File metadata and controls
40 lines (30 loc) · 937 Bytes
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
#include <iostream>
#include <cstring>
const int MAX = 1000;
using namespace std;
int lcs(const char* X, const char* Y, int m, int n, int memo[MAX][MAX]) {
if (memo[m][n] != -1) {
return memo[m][n];
}
if (m == 0 || n == 0) {
memo[m][n] = 0;
} else if (X[m - 1] == Y[n - 1]) {
memo[m][n] = 1 + lcs(X, Y, m - 1, n - 1, memo);
} else {
memo[m][n] = max(lcs(X, Y, m - 1, n, memo), lcs(X, Y, m, n - 1, memo));
}
return memo[m][n];
}
int longestCommonSubsequence(const char* X, const char* Y) {
int m = strlen(X);
int n = strlen(Y);
int memo[MAX][MAX];
memset(memo, -1, sizeof(memo));
return lcs(X, Y, m, n, memo);
}
int main() {
const char* X = "ABCBDAB";
const char* Y = "BDCAB";
int result = longestCommonSubsequence(X, Y);
cout << "Length of Longest Common Subsequence: " << result << endl;
}