-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlongest_common_substring.cpp
More file actions
39 lines (34 loc) · 1.11 KB
/
Copy pathlongest_common_substring.cpp
File metadata and controls
39 lines (34 loc) · 1.11 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
class Solution {
public:
/**
* @param A, B: Two string.
* @return: the length of the longest common substring.
*/
int longestCommonSubstring(string &A, string &B) {
// write your code here
// f[i][j] lenght of first elements of A and first j elements of B
// with i and j as its last charactor of substring
// f[0][j] = 0; f[i][0] = 0;
// f[i][j] = 0 if A[i - 1] != B[i - 1]
int len_a = A.size();
int len_b = B.size();
if (len_a <= 0 || len_b <= 0) {
return 0;
}
vector<vector<int>> f(len_a + 1, vector<int> (len_b + 1, 0));
int lcs_length = 0;
for (int i = 1; i < len_a + 1; i++) {
for (int j = 1; j < len_b + 1; j++) {
if (A[i - 1] == B[j - 1]) {
f[i][j] = f[i - 1][j - 1] + 1;
} else {
f[i][j] = 0;
}
if (f[i][j] > lcs_length) {
lcs_length = f[i][j];
}
}
}
return lcs_length;
}
};