Repository navigation
Expand file tree
/
Copy pathdistinct_subsequences.cpp
More file actions
139 lines (114 loc) · 3.37 KB
/
Copy pathdistinct_subsequences.cpp
File metadata and controls
139 lines (114 loc) · 3.37 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include <string>
#include <vector>
#include <list>
#include <iostream>
using namespace std;
/***********************************************
the first solution uses too much memory
************************************************/
class FirstSolution {
public:
int numDistinct(string s, string t) {
int len = s.length();
int sub_len = t.length();
// 缓存下所有可能的字符串
list<string> possible_strs;
vector<int> dp(len + 1, 0);
list<string> copy_list;
for (int i = 0; i < len; i ++)
{
char cur_ch = s[i];
string empty_str;
int match_num = 0;
copy_list.clear();
// 对所有可能的字符串进行遍历,看看能产生多少的匹配量,并更新可能的字符串list
for (list<string>::iterator iter = possible_strs.begin(); iter != possible_strs.end(); iter ++)
{
iter->append(1, cur_ch);
int str_len = iter->length();
if (iter->compare(0, str_len, t, 0, str_len) != 0)
{
*iter = iter->substr(0, str_len - 1);
str_len = iter->length();
for (int m = str_len - 1; m >= 0; m --)
{
if ((*iter)[m] != cur_ch)
{
break;
}
copy_list.push_back(*iter);
}
continue;
}
if (str_len == sub_len)
{
match_num++;
}
}
for (list<string>::iterator iter = copy_list.begin(); iter != copy_list.end(); iter ++)
{
possible_strs.push_back(*iter);
}
if (s[i] == t[0])
{
string new_str;
new_str.assign(1, s[i]);
possible_strs.push_back(new_str);
}
dp[i + 1] = dp[i] + match_num;
}
return dp[len];
}
};
class Solution {
public:
int numDistinct(string s, string t) {
int len = s.length();
int sub_len = t.length();
vector<vector<int> > dp;
// 初始化二维数组
vector<int> tmp;
for (int i = 0; i <= len; i ++)
{
tmp.clear();
for (int j = 0; j <= sub_len; j ++)
{
if (j == 0)
{
// 初始化,当子串为空串,只有一种情况可以转换,就是把自已的数据删除掉
tmp.push_back(1);
}
else
{
tmp.push_back(0);
}
}
dp.push_back(tmp);
}
for (int i = 0; i < len; i ++)
{
for (int j = 0; j < sub_len; j ++)
{
if (s[i] == t[j])
{
dp[i + 1][j + 1] = dp[i][j + 1] + dp[i][j];
}
else
{
dp[i + 1][j + 1] = dp[i][j + 1];
}
}
}
return dp[len][sub_len];
}
};
int main(int argc, char **argv)
{
Solution test;
string s = "aacaacca";
string t = "ca";
int ret = test.numDistinct(s, t);
cout << ret << endl;
int f;
cin >> f;
}