-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path10.cpp
254 lines (240 loc) · 6.75 KB
/
10.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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
__________________________________________________________________________________________________
4ms
class Solution {
public:
bool isMatch(string s, string p) {
const int m = s.size(), n = p.size();
int i = 0, j = 0;
bool dp[n+1][m+1]; // i - n - p, j - m - s
memset(dp, false, sizeof(bool)*(m+1)*(n+1)); // i at string corresponds with i+1 at dp matrix
dp[0][0] = true;
while (j++ < n){ // scan * while i = 0
if (j > 1 && p[j-1] == '*') dp[j][0] = dp[j-2][0];
}
while (i++ < m){
j = 0;
while (j++ < n){
if (p[j-1] == s[i-1] || p[j-1] == '.') dp[j][i] = dp[j-1][i-1]; // if not * but match
else if (p[j-1] == '*'){ // if * |
if (p[j-2] != s[i-1] && p[j-2] != '.') dp[j][i] = dp[j-2][i]; // | not match
else dp[j][i] = (dp[j-2][i] || dp[j-1][i] || dp[j][i-1]); // | match
}
}
}
return dp[n][m];
}
};
__________________________________________________________________________________________________
8ms
class Solution {
public:
bool isMatch(string s, string p) {
int m = s.size(), n = p.size();
vector<vector<bool>> dp(m+1, vector<bool>(n+1, false));
dp[0][0] = true;
for(int i=0; i<=m; ++i)
{
for(int j=1; j<=n; ++j)
{
if(p[j-1] == '*')
{
if(j>=2 && dp[i][j-2])
dp[i][j] = true; // repeat 0
if(j>=2 && i>=1)
if(p[j-2] == s[i-1] || p[j-2] == '.')
if(dp[i-1][j] == true)
dp[i][j] = true;
}
else
{
if(i>=1 && (s[i-1]==p[j-1] || p[j-1] == '.'))
dp[i][j] = dp[i-1][j-1];
}
}
}
return dp[m][n];
}
};
__________________________________________________________________________________________________
12ms
class Solution {
public:
bool isMatch(string s, string p)
{
dp.resize(s.size() + 1);
for(auto& it : dp)
{
it.resize(p.size(), -1);
}
return helper(s, 0, p, 0);
}
private:
vector<vector<int>> dp;
int real_end;
bool helper(const string& S, int si, const string& P, int pi)
{
if(si > S.size())
{
return pi >= P.size();
}
if(pi >= P.size())
{
return si >= S.size();
}
if(dp[si][pi] != -1)
{
return dp[si][pi] == 1;
}
char s = si < S.size() ? S[si] : 0;
char p = P[pi];
bool ret = false;
if(s != 0 && (s == p || p == '.'))
{
ret = helper(S, si + 1, P, pi + 1);
}
else if(p == '*')
{
char prev = pi > 0 ? P[pi-1] : 0;
if(prev == 0)
{
ret = false;
}
else
{
ret = helper(S, si, P, pi + 1);
if(prev == '.' || prev == s)
{
ret |= helper(S, si + 1, P, pi);
}
}
}
if(pi < P.size() - 1 && P[pi + 1] == '*')
{
ret |= helper(S, si, P, pi + 2);
}
dp[si][pi] = ret ? 1 : 0;
return ret;
}
};
__________________________________________________________________________________________________
8228 kb
static int pre = []() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
return 0;
}();
class Solution {
public:
bool isMatchRecursive(const string& s, const string& p, int strIndex, int patternIndex) {
while (strIndex < s.size() && patternIndex < static_cast<int>(p.size() - 1)) {
if (p[patternIndex + 1] != '*') {
if (p[patternIndex] != '.') {
if (s[strIndex] == p[patternIndex]) {
strIndex++;
patternIndex++;
}
else {
return false;
}
}
else {
strIndex++;
patternIndex++;
}
}
else {
if (p[patternIndex] != '.') {
while (true) {
bool success = isMatchRecursive(s, p, strIndex, patternIndex + 2);
if (success)
return true;
else if (strIndex < s.size() && s[strIndex] == p[patternIndex])
strIndex++;
else
break;
}
patternIndex += 2;
}
else {
do {
bool success = isMatchRecursive(s, p, strIndex, patternIndex + 2);
if (success)
return true;
else
strIndex++;
} while (strIndex < s.size());
patternIndex += 2;
}
}
}
if (strIndex < s.size() && patternIndex == p.size() - 1) {
if (p[patternIndex] != '.') {
if (s[strIndex] == p[patternIndex]) {
strIndex++;
patternIndex++;
}
else {
return false;
}
}
else {
strIndex++;
patternIndex++;
}
}
if (patternIndex >= p.size()) {
if (strIndex >= s.size())
return true;
else
return false;
}
else {
while (patternIndex < (int)p.size() - 1) {
if (p[patternIndex + 1] != '*') {
return false;
}
else {
patternIndex += 2;
}
}
if (patternIndex == p.size() - 1) {
return false;
}
return true;
}
}
bool isMatch(string s, string p) {
return isMatchRecursive(s, p, 0, 0);
}
};
__________________________________________________________________________________________________
9084 kb
static int x = [](){
std::ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
return NULL;
}();
class Solution {
public:
bool isMatch(string s, string p) {
int s_l = s.length(), p_l = p.length();
int maxn = 1000+10;
int d[maxn][maxn];
for(int i = 0; i<=s_l; i++){
memset(d[i], 0, sizeof(int)*maxn);
}
//d[i][j]: s[i-1] and p[j-1] match
d[0][0] = 1;
for(int i = 0; i<=s_l; i++){
for(int j = 0; j<=p_l; j++){
if(j>=1 && d[i][j-1] && p[j-1]=='*') d[i][j] = 1;
if(i>=1 && j>=2 && d[i-1][j] && p[j-1] == '*' && (s[i-1] == p[j-2] || p[j-2] == '.')) d[i][j] = 1;
if(i>=1 && j>=1 && d[i-1][j-1] && (s[i-1]==p[j-1] || p[j-1] == '.')) d[i][j] = 1;
if(j>=2 && d[i][j-2] && p[j-1] == '*') d[i][j] =1;
}
}
return (bool)d[s_l][p_l];
}
};
__________________________________________________________________________________________________