-
Notifications
You must be signed in to change notification settings - Fork 0
/
q10_memory_exceed.cpp
158 lines (145 loc) · 2.76 KB
/
q10_memory_exceed.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
static const auto __ = []() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
return nullptr;
}();
struct patt {
char match=0;
int mode=-1;
int p;
patt() {}
~patt() {}
void setp(int pin) { p = pin; }
void setpatt(char matin, char modin) {
match = matin;
if (modin == '*') mode = 1;
}
void setpatt() {
mode = -1;
}
void setpatt(char matin) {
match = matin;
mode = 0;
}
int matchq() { return mode; }
int matchq(char in) {
if (mode == -1) return 0;//代表需要读下一个patt
if (match == '.' || in == match) {
if (mode == 0) {
mode = -1;
return 1;//表示成功匹配
}
if (mode == 1) {
return 2;//代表分叉
}
}
if (mode == 1 && in != match) {
mode = -1;
return 0;//代表需要读下一个patt
}
return -1;//代表不匹配
}
int readpatt(string& s) {
if (p >= s.size()) return -1;
if (p == s.size() - 1) {
setpatt(s[p]);
p++;
return 0;
}
if (p + 1<s.size()) {
if (s[p + 1] != '*') {
if ((s[p]>64 && s[p]<91) || (s[p]>96 && s[p]<123) || s[p] == '.') {
setpatt(s[p]);
p++;
}
else {
mode = -1;
p++;
}
}
else if (s[p + 1] == '*') {
if ((s[p]>64 && s[p]<91) || (s[p]>96 && s[p]<123) || s[p] == '.') {
setpatt(s[p], '*');
p += 2;
}
else {
mode = -1;
p += 2;
}
}
return 0;
}
}
};
class Solution {
public:
bool isMatch(string s, string p) {
list<patt> pattlist;
int sp(0), pp(0);
patt initpatt;
initpatt.setp(sp);
pattlist.push_back(initpatt);
while (sp < s.size()) {
for (list<patt>::iterator it = pattlist.begin(); it != pattlist.end();) {
while (true) {
int index = it->matchq(s[sp]);
if (index == -1) {
it = pattlist.erase(it);
if (pattlist.empty()) {
it = pattlist.end();
}
break;//deleted
}
if (index == 0) {
if (it->readpatt(p) == -1) {
it = pattlist.erase(it);
if (pattlist.empty()) {
it = pattlist.end();
}
break;//deleted
}
}
if (index == 1) {
if (it != pattlist.end()) {
++it;
}
break;//mode:-1
}
if (index == 2) {
patt temppatt = *it;
temppatt.setpatt();
pattlist.push_back(temppatt);
if (it != pattlist.end()) {
++it;
}
break;//mode:1
}
}
}
sp++;
}
for (list<patt>::iterator it = pattlist.begin(); it != pattlist.end();) {
if (it->p == p.size()) return true;
while (true) {
if (it->readpatt(p)==-1) {
if (it->matchq() == 1) {
return true;
}
else {
if (it != pattlist.end()) {
++it;
}
break;
}
}
if (it->matchq() != 1) {
if (it != pattlist.end()) {
++it;
}
break;
}
}
}
return false;
}
};