-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy path0591.cpp
More file actions
97 lines (95 loc) · 2.2 KB
/
0591.cpp
File metadata and controls
97 lines (95 loc) · 2.2 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
class Solution {
public:
bool isValid(string code) {
if (code.empty()) {
return false;
}
if (code[0] != '<') {
return false;
}
int l = 0;
int r = 0;
while (r < code.size() && code[r] != '>') {
++r;
}
string outter_tag_first = code.substr(l, r - l + 1);
if (!valid(outter_tag_first)) {
return false;
}
string outter_tag_second{outter_tag_first};
outter_tag_second.insert(outter_tag_second.begin() + 1, '/');
int search_end = code.size() - outter_tag_second.size();
if (search_end < 0) {
return false;
}
if (code.substr(search_end, outter_tag_second.size()) !=
outter_tag_second) {
return false;
}
string cdata_prefix{"<![CDATA["};
string cdata_suffix{"]]>"};
stack<string> s;
l = outter_tag_first.size();
while (l < search_end) {
while (code[l] != '<') {
++l;
}
if (code.substr(l, cdata_prefix.size()) == cdata_prefix) {
int pos = code.find(cdata_suffix, l + cdata_prefix.size());
if (pos == string::npos) {
return false;
}
l = pos + cdata_suffix.size();
continue;
}
if (l >= search_end) {
break;
}
r = l;
while (r < search_end && code[r] != '>') {
++r;
}
string tag = code.substr(l, r - l + 1);
if (tag[1] == '/') {
if (s.empty() || !compare(s.top(), tag)) {
return false;
}
s.pop();
} else {
if (!valid(tag)) {
return false;
}
s.emplace(tag);
}
l = r + 1;
}
return s.empty();
}
private:
bool compare(const string& a, const string& b) {
if (a[0] != '<' || b[0] != '<' || b[1] != '/') {
return false;
}
for (int i = 1; i < a.size(); ++i) {
if (a[i] != b[i + 1]) {
return false;
}
}
return true;
}
bool valid(const string& tag) {
int sz = tag.size();
if (sz <= 2 || sz > 11) {
return false;
}
if (tag[0] != '<' || tag[sz - 1] != '>') {
return false;
}
for (int i = 1; i < sz - 1; ++i) {
if (tag[i] < 'A' || tag[i] > 'Z') {
return false;
}
}
return true;
}
};