-
Notifications
You must be signed in to change notification settings - Fork 0
/
10.regular-expression-matching.57779619.ac.cpp
126 lines (123 loc) · 2.69 KB
/
10.regular-expression-matching.57779619.ac.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
/*
* [10] Regular Expression Matching
*
* https://leetcode.com/problems/regular-expression-matching
*
* Hard (24.04%)
* Total Accepted:
* Total Submissions:
* Testcase Example: '"aa"\n"a"'
*
* Implement regular expression matching with support for '.' and '*'.
*
*
* '.' Matches any single character.
* '*' Matches zero or more of the preceding element.
*
* The matching should cover the entire input string (not partial).
*
* The function prototype should be:
* bool isMatch(const char *s, const char *p)
*
* Some examples:
* isMatch("aa","a") → false
* isMatch("aa","aa") → true
* isMatch("aaa","aa") → false
* isMatch("aa", "a*") → true
* isMatch("aa", ".*") → true
* isMatch("ab", ".*") → true
* isMatch("aab", "c*a*b") → true
*
*/
class Solution {
int curr = 0;
int i = 0;
//ppos spos len
stack<tuple<int, int, int>> stk;
bool backTrace()
{
while (1) {
if (stk.empty()) {
return false;
}
auto& top = stk.top();
if (get<2>(top) == 0) {
stk.pop();
} else {
i = get<0>(top);
--get<2>(top);
curr = get<1>(top) + get<2>(top);
//cout << i << ' ' << curr << ' ' << get<2>(top) << endl;
break;
}
}
return true;
}
public:
bool isMatch(string s, string p) {
for (; i < p.length();) {
//cout << endl << curr << " " << i << endl;
if (p[i] == '.' && curr < s.length()) {
if (p[i + 1] == '*') {
stk.push(make_tuple(i + 2, curr, s.length() - curr));
curr = s.length();
i += 2;
} else {
curr++;
i += 1;
}
} else if (isalpha(p[i]) && curr < s.length()) {
if (p[i + 1] == '*') {
int maxpos = curr;
while (s[maxpos] == p[i]) {
maxpos++;
}
//cout << p[i] << ' ' << maxpos << ' ' << curr << endl;
stk.push(make_tuple(i + 2, curr, maxpos - curr));
curr = maxpos;
i += 2;
} else {
//cout << "c: " << curr << "i " << i << endl;
if (p[i] != s[curr]) {
if (!backTrace()) {
return false;
} else {
continue;
}
} else {
curr++;
i += 1;
}
}
}
bool a = (i >= p.length());
bool b = (curr >= s.length());
bool flag = false;
if (b) {
int tmpi = i;
for (; tmpi + 1 <= p.length(); tmpi+=2) {
//cout << tmpi << endl;
if (p[tmpi + 1] != '*') {
flag = true;
break;
}
}
}
//cout << i;
if (b && flag) {
if (!backTrace()) {
return false;
} else {
continue;
}
} else {
if (b && !flag) {
return true;
} else {
continue;
}
}
}
return curr == s.length();
}
};