-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathLongest_Common_Subsequence.cpp
59 lines (48 loc) · 1.56 KB
/
Longest_Common_Subsequence.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
/*
Given two strings, find the longest comment subsequence (LCS).
Your code should return the length of LCS.
Example
For "ABCD" and "EDCA", the LCS is "A" (or D or C), return 1
For "ABCD" and "EACB", the LCS is "AC", return 2
*/
#include <string>
using namespace std;
class Solution {
public:
/**
* @param A, B: Two strings.
* @return: The length of longest common subsequence of A and B.
*/
/* first, think as recursion
int longestCommonSubsequence(string A, string B) {
// write your code here
if (A.empty() || B.empty()) {
return 0;
} else if (A[A.length() - 1] == B[B.length() - 1]) {
return 1 + longestCommonSubsequence(A.substr(0, A.length() - 1), B.substr(0, B.length() - 1));
} else {
return max(longestCommonSubsequence(A.substr(0, A.length() - 1), B), longestCommonSubsequence(A, B.substr(0, B.length() - 1)));
}
}
ok, now convert it to dp
*/
int longestCommonSubsequence(string A, string B) {
// write your code here
int m = A.length();
int n = B.length();
int dp[m + 1][n + 1];
int i, j;
for (i = 0; i <= m; i++) {
for (j = 0; j <= n; j++) {
if (i == 0 || j == 0) {
dp[i][j] = 0;
} else if (A[i - 1] == B[j - 1]) {
dp[i][j] = dp[i - 1][j - 1] + 1;
} else {
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
}
}
}
return dp[m][n];
}
};