Repository navigation
Expand file tree
/
Copy path04Subsequences.cpp
More file actions
76 lines (64 loc) · 1.63 KB
/
Copy path04Subsequences.cpp
File metadata and controls
76 lines (64 loc) · 1.63 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <bits/stdc++.h>
using namespace std;
void printArray(vector<int> &v)
{
for (auto &it : v)
cout << it << "\n";
cout << "\n";
}
int n, m;
vector<vector<int>> dp;
int func(string &s1, string &s2, int i, int j)
{
if (i > n || j > m)
return 0;
if (dp[i][j] != -1)
return dp[i][j];
if (s1[i] == s2[j])
{
return dp[i][j] = 1 + func(s1, s2, i + 1, j + 1);
}
else
{
int r1 = func(s1, s2, i, j + 1);
int r2 = func(s1, s2, i + 1, j);
return dp[i][j] = max(r1, r2);
}
}
/*
* In the iterative solution we didn't need a dp of m+1 and n+1 size even if we have written
* But in the case of tabulation we need it
* The reason is **index shifting**
* Index shifting happens when the base case of our recursive code is less than 0
* As in tabulation we can't write less than 0 base case. Therefore we add an extra index.
*/
int longestCommonSubsequence(string s1, string s2)
{
dp.resize(n + 1, vector<int>(m + 1, 0));
for (int i = n - 1; i >= 0; --i)
{
for (int j = m - 1; j >= 0; --j)
{
if (s1[i] == s2[j])
{
dp[i][j] = 1 + dp[i + 1][j + 1];
}
else
{
int r1 = dp[i][j + 1];
int r2 = dp[i + 1][j];
dp[i][j] = max(r1, r2);
}
}
}
return dp[0][0];
}
int main()
{
string s1 = "abcde", s2 = "ace";
n = s1.size(), m = s2.size();
dp.resize(n + 1, vector<int>(m + 1, -1));
cout << func(s1, s2, 0, 0) - 1 << "\n";
cout << longestCommonSubsequence(s1, s2);
return 0;
}