-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdistinct_subsequences.cpp
More file actions
40 lines (37 loc) · 1.16 KB
/
Copy pathdistinct_subsequences.cpp
File metadata and controls
40 lines (37 loc) · 1.16 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
class Solution {
public:
/**
* @param S, T: Two string.
* @return: Count the number of distinct subsequences
*/
int numDistinct(string &S, string &T) {
// write your code here
// f[i][j] first i characters of S, with first j characters of T. the
// count of distinct subsequences
// f[i][0] = 1;
// f[0][j] = 0
// S[i - 1] == T[i - 1]
// f[i][j] = f[i - 1][j - 1]
// if (S[i -1] != T[i -1])
// f[i][j] = f[i - 1][j]
// S[i- 1]是否用于与T[i-1]match
int len_s = S.size();
int len_t = T.size();
vector<vector<int>> f(len_s + 1, vector<int>(len_t + 1, 0));
for (int i = 0; i < len_s + 1; i++) {
f[i][0] = 1;
}
for (int j = 1; j < len_t + 1; j++) {
f[0][j] = 0;
}
for (int i = 1; i < len_s + 1; i++) {
for (int j = 1; j < min(len_t + 1, i + 1); j++) {
f[i][j] = f[i - 1][j];
if (S[i - 1] == T[j - 1]) {
f[i][j] += f[i - 1][j - 1];
}
}
}
return f[len_s][len_t];
}
};