-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlongest_common_subsequence.cpp
More file actions
68 lines (57 loc) 路 1.96 KB
/
Copy pathlongest_common_subsequence.cpp
File metadata and controls
68 lines (57 loc) 路 1.96 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
// https://leetcode.com/problems/longest-common-subsequence/
class Solution
{
public:
int longestCommonSubsequence(string text1, string text2)
{
vector<vector<int>> dp(text1.length() + 1, vector<int>(text2.length() + 1, 0));
CalcLength(dp, text1, text2);
return dp[text1.length()][text2.length()];
}
void CalcLength(vector<vector<int>> &dp, string &text1, string &text2)
{
for (int i = 1; i < dp.size(); i++)
{
for (int j = 1; j < dp[0].size(); j++)
{
if (text1[i - 1] == text2[j - 1])
dp[i][j] = dp[i - 1][j - 1] + 1;
else if (dp[i - 1][j] >= dp[i][j - 1])
dp[i][j] = dp[i - 1][j];
else
dp[i][j] = dp[i][j - 1];
}
}
}
// MEMOIZED VERSION - TLE!
class Solution {
public:
int longestCommonSubsequence(string text1, string text2) {
vector<vector<int>> dp(text1.length()+1, vector<int> (text2.length() + 1, 0));
CalcLength(dp, text1, text2, text1.length(), text2.length());
return dp[text1.length()][text2.length()];
}
int CalcLength(vector<vector<int>>& dp, string& text1, string& text2, int i, int j){
if (i < 1 || j < 1) return 0;
if(text1[i-1] == text2[j-1]) {
dp[i][j] = 1 + CalcLength(dp, text1, text2, i-1, j-1);
} else {
dp[i][j] = max(CalcLength(dp, text1, text2, i-1, j), CalcLength(dp, text1, text2, i, j-1));
}
return dp[i][j];
}
template <class T>
void PrintMatrix(vector<vector<T>> &matrix)
{
for (int i = 0; i < matrix.size(); i++)
{
cout << "| ";
for (int j = 0; j < matrix[0].size(); j++)
{
cout << matrix[i][j] << " | ";
}
cout << endl;
}
cout << "-----" << endl;
}
};