-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathLongest_common_subsequence.cpp
More file actions
49 lines (38 loc) · 1.25 KB
/
Copy pathLongest_common_subsequence.cpp
File metadata and controls
49 lines (38 loc) · 1.25 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
// Longest Common Subsequence//
// Using Space Optimisation
/*If we closely we are using two rows: dp[ind1-1][ ], dp[ind][ ],
So we are not required to contain an entire array, we can simply have two rows prev and cur where prev corresponds to dp[ind-1] and cur to dp[ind].
After declaring prev and cur, replace dp[ind-1] to prev and dp[ind] with cur and after the inner loop executes, we will set prev = cur, so that the cur row can serve as prev for the next index.*/
#include <bits/stdc++.h>
using namespace std;
int lcs(string s1, string s2)
{
int n = s1.size();
int m = s2.size();
vector<vector<int>> dp(n + 1, vector<int>(m + 1, -1));
for (int i = 0; i <= n; i++)
{
dp[i][0] = 0;
}
for (int i = 0; i <= m; i++)
{
dp[0][i] = 0;
}
for (int ind1 = 1; ind1 <= n; ind1++)
{
for (int ind2 = 1; ind2 <= m; ind2++)
{
if (s1[ind1 - 1] == s2[ind2 - 1])
dp[ind1][ind2] = 1 + dp[ind1 - 1][ind2 - 1];
else
dp[ind1][ind2] = 0 + max(dp[ind1 - 1][ind2], dp[ind1][ind2 - 1]);
}
}
return dp[n][m];
}
int main()
{
string s1 = "acd";
string s2 = "ced";
cout << "The Length of Longest Common Subsequence is " << lcs(s1, s2);
}