-
Notifications
You must be signed in to change notification settings - Fork 1
/
16_LCS_memoization.cpp
69 lines (53 loc) · 1.68 KB
/
16_LCS_memoization.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
#include <bits/stdc++.h>
using namespace std;
int t[1001][1001];
int LCS(string X, string Y, int n, int m) {
// base case
if (n == 0 || m == 0) {
return 0;
}
if (t[n][m] != -1) {
return t[n][m];
}
// choice diagram
if (X[n - 1] == Y[m - 1]) { // when last character is same
t[n][m] = 1 + LCS(X, Y, n - 1, m - 1); // count the number and decreament the both's string length // store the value in particular block
}
else { // when last character is not same -> pick max
t[n][m] = max(LCS(X, Y, n - 1, m), LCS(X, Y, n, m - 1)); // one take full and another by leaving last char and vice versa // store the value in particular block
}
return t[n][m];
}
int main() {
string X, Y;
cin >> X >> Y;
int n = X.length(), m = Y.length();
memset(t, -1, sizeof(t)); // intialize the whole dp matrix with -1; // from memset we can initialise either -1 and zero;
cout << LCS(X, Y, n, m) << endl;
return 0;
}
// OR
static int dp[1001][1001];
class Solution {
public:
int test(int x, int y, string &s1, string &s2) {
if(x == 0 || y == 0) {
return 0;
}
if(dp[x][y] != -1) {
return dp[x][y];
}
if(s1[x-1] == s2[y-1]) {
return dp[x][y] = 1 + test(x-1,y-1,s1,s2);
}
else {
return dp[x][y] = max(test(x-1, y, s1, s2), test(x, y-1, s1, s2));
}
}
//Function to find the length of longest common subsequence in two strings.
int lcs(int x, int y, string s1, string s2) {
memset(dp, -1, sizeof(dp));
int p = test(x, y, s1, s2);
return p;
}
};