-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegularExpressionMatching.cpp
More file actions
110 lines (105 loc) · 3.3 KB
/
RegularExpressionMatching.cpp
File metadata and controls
110 lines (105 loc) · 3.3 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
/*
_ooOoo_
o8888888o
88" . "88
(| -_- |)
O\ = /O
____/`---'\____
.' \\| |// `.
/ \\||| : |||// \
/ _||||| -:- |||||- \
| | \\\ - /// | |
| \_| ''\---/'' | |
\ .-\__ `-` ___/-. /
___`. .' /--.--\ `. . __
."" '< `.___\_<|>_/___.' >'"".
| | : `- \`.;`\ _ /`;.`/ - ` : | |
\ \ `-. \_ __\ /__ _/ .-` / /
======`-.____`-.___\_____/___.-`____.-'======
`=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
God Bless Me BUG Free Forever
*/
/***
* 递归 + 回溯
* 由于'*'和前一个元素相关,所以每次判断后一个元素*(p+1)
* 完全匹配的条件是s p 都到达末尾
* 若*(p+1) != '*',则前一个必须匹配(相等或者有'.')
* == '*',若前一个不匹配,则比较(s, p+2) 跳过p和p+1
* 匹配,则比较(s+1, p) 跳过s || (s, p+2) 跳过'*'
* 注意:'*'匹配前一个元素的0个或多个
***/
/*
class Solution {
public:
bool isMatch(const char *s, const char *p) {
if ('\0' == *p) // the end
return ('\0' == *s);
if (*(p+1) != '*')
{
if (('\0' != *s) && ((*s == *p) || (*p == '.')))
return isMatch(s+1, p+1);
else
return false;
}
else
{
if (('\0' != *s) && ((*s == *p) || (*p == '.')))
return isMatch(s+1, p) || isMatch(s, p+2); // use '*' || jump '*'
else
return isMatch(s, p+2); // jump '*'
}
}
};
*/
/***
* 法2:在法1的基础上 另一种递归
* 当*(p+1) == '*',此时看从s[i]开始的子串
* 假设s[i],s[i+1],...s[i+k]都等于p[j]那么意味着这些都有可能是合适的匹配
* 那么递归对于剩下的(i,j+2),(i+1,j+2),...,(i+k,j+2)都要尝试(j+2是因为跳过当前和下一个'*'字符)。
***/
/*
class Solution {
public:
bool isMatch(const char *s, const char *p) {
if ('\0' == *p)
return ('\0' == *s);
if ('*' != *(p+1))
{
if (('\0' != *s) && ((*s == *p) || (*p == '.')))
return isMatch(s+1, p+1);
else
return false;
}
else
{
while (('\0' != *s) && ((*s == *p) || (*p == '.')))
{
if (isMatch(s, p+2)) // jump '*'
return true;
++s; // next s
}
return isMatch(s, p+2); // pre not match, have to jump '*'
}
}
};
*/
/***
* 法3:另外一种递归逻辑,判断当前元素*p(法1是先判断*(p+1))
* 若*p == *s,若*(p+1) != '*',直接后移,比较(s+1, p+1)
* == 使用'*'通配 (s+1, p) || 不使用'*' (s, p+2)
* != != false
* == 跳过 比较(s, p+2)
***/
class Solution {
public:
bool isMatch(const char *s, const char *p) {
if ('\0' == *p)
return ('\0' == *s);
if (('\0' != *s) && ((*p == *s) || ('.' == *p)))
return (*(p+1) == '*') ? (isMatch(s+1, p) || isMatch(s, p+2)) : // use '*' || not use '*'
isMatch(s+1, p+1); // match move on
else
return (*(p+1) == '*') ? isMatch(s, p+2) : false; // not use '*'
}
};