forked from illuz/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAC_dfs_n!.cpp
More file actions
40 lines (34 loc) · 996 Bytes
/
AC_dfs_n!.cpp
File metadata and controls
40 lines (34 loc) · 996 Bytes
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
/*
* Author: illuz <iilluzen[at]gmail.com>
* File: AC_bruteforce_n!.cpp
* Create Date: 2014-12-01 08:11:49
* Descripton: I guess its complexity is n! (worst)
* if p[j+1] == '*' -> (i+1,j+1)
* else if p[i] == p[j] -> (i+1, j+2) or (i, j+2)
* else -> (i, j+2)
*/
#include <bits/stdc++.h>
using namespace std;
const int N = 0;
class Solution {
public:
bool isMatch(const char *s, const char *p) {
if (!p[0])
return !s[0];
int slen = strlen(s), plen = strlen(p);
if (plen == 1 || p[1] != '*')
return slen && (p[0] == '.' || s[0] == p[0])
&& isMatch(s + 1, p + 1);
while (s[0] && (p[0] == '.' || s[0] == p[0]))
if (isMatch(s++, p + 2))
return true;
return isMatch(s, p + 2);
}
};
int main() {
Solution s;
char a[100], b[100];
while (cin >> a >> b)
cout << s.isMatch(a, b) << endl;
return 0;
}