-
Notifications
You must be signed in to change notification settings - Fork 0
/
115-DistinctSubsequences.cpp
51 lines (46 loc) · 1.32 KB
/
115-DistinctSubsequences.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
/*=============================================================================
# FileName: 115-DistinctSubsequences.cpp
# Desc:
# Author: qsword
# Email: huangjian1993@gmail.com
# HomePage:
# Created: 2015-05-08 18:37:31
# Version: 0.0.1
# LastChange: 2015-05-08 19:02:40
# History:
# 0.0.1 | qsword | init
=============================================================================*/
#include <stdio.h>
#include <vector>
#include <string>
using namespace std;
class Solution {
public:
//38ms
int numDistinct(string s, string t) {
if (t == "") {
return 1;
}
int len1 = s.length(), len2 = t.length();
if (len1 < len2) {
return false;
}
vector<vector<int> > dp(len1 + 1, vector<int>(len2 + 1, 0));
for (int i = 0; i <= len2; i ++) {
dp[0][i] = 0;
}
for (int i = 0; i <= len1; i ++) {
dp[i][0] = 1;
}
for (int j = 1; j <= len2; j ++) {
for (int i = j; i <= len1; i ++) {
dp[i][j] = s[i - 1] == t[j - 1] ? dp[i - 1][j - 1] + dp[i - 1][j]: dp[i - 1][j];
}
}
return dp[len1][len2];
}
};
int main() {
Solution solution;
printf("%d\n", solution.numDistinct("rabbbit", "rabbit"));
}