forked from moranzcw/LeetCode-NOTES
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.cpp
123 lines (118 loc) · 4.04 KB
/
solution.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
class Solution
{
public:
bool isMatch(const char *s, const char *p)
{
//greedy
bool pre,next;
const char *start,*end,*mid;
const char *ss,*se,*sm;
start = p;
end = p;
ss = s;
while(true)
{
if(*start == '*')
pre = true;//查看前面有没有*
else
pre = false;
//查看新的没有*的word
while(*start != '\0' && *start == '*')
start++;
end = start;
while(*end != '\0' && *end != '*')
end++;
if(*end == '*')
next = true;
else
next = false;
end--;
if(*ss == '\0' && pre && *start == '\0')
return true;//p和s均完全匹配
if(*ss != '\0' && pre && *start == '\0')
return true;
if(*ss != '\0' && (*start == '\0' && !pre))
return false;//s没能完全匹配
if(*ss == '\0' && *start != '\0')
return false;//p的字母没能匹配完全
//处理greedy匹配问题
if(next == true)
{//后面有*可供匹配
if(pre == true)
{//前面有供做匹配的
sm = ss,mid = start;//判断s中剩余的还够匹配不够
while(mid != end && *sm != '\0')
{
mid++,sm++;
}
if(*sm == '\0')
return false;
mid = start;
while(*mid != '*' && *ss != '\0')
{
for(sm = ss,mid = start;*sm != '\0' && *mid != '*';sm++,mid++)
{
if(*mid == '?')continue;
if(*mid != *sm)break;
}
if(*mid == '*')
ss = sm;
else
ss++;
}//while
if(*mid != '*')
return false;
ss = sm;
start = mid;
}
else
{//前面没有供匹配的
for(sm = ss,mid = start;*sm != '\0' && mid != end+1;sm++,mid++)
{
if(*mid == '?')
continue;
if(*mid != *sm)
return false;
}
if(mid != end+1)
return false;
ss = sm;//匹配成功
start = end+1;
}
}
else
{//最后必须匹配的一串,因为后面没有*可供匹配最后的字母
if(pre)
{
se = ss;
while(*se != '\0')
se++;
se--;
for(sm = se,mid = end;mid != start && sm != ss;mid--,sm--)
{
if(*mid == '?')
continue;
if(*mid != *sm)
return false;
}
if((sm == ss && mid != start) || (*mid != *sm && *mid != '?'))
return false;//如果s不够匹配或者p开头不能匹配
return true;
}
else
{
for(sm = ss,mid = start;*sm != '\0' && *mid !='\0';sm++,mid++)
{
if(*mid == '?')
continue;
if(*mid != *sm)
return false;
}
if(*sm != '\0' || *mid != '\0')
return false;
return true;
}
}
}
}
};