forked from daiwb/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1116A Well-Formed Problem.cpp
More file actions
177 lines (168 loc) · 3.92 KB
/
1116A Well-Formed Problem.cpp
File metadata and controls
177 lines (168 loc) · 3.92 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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/**
* P1010: A Well-formed Program
*
* ZJU Summer Camp 2003
*
* Judge Solution created by WishingBone
*
* Algo Desc: Stack + String Handling
*/
#ifdef WIN32
# pragma warning(disable:4786)
# define for if (0); else for
#endif
#include <iostream>
#include <cstdio>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
// tag types enumeration
enum tagtype {
begin,
end,
standalone
};
// tag info
class tag {
public:
string name;
tagtype type;
tag(string name, tagtype type) : name(name), type(type) {
}
};
// read a tag content
string tagcontent() {
char ch;
do {
ch = getchar();
} while (ch != '<');
string ret = "";
ch = getchar();
while (ch != '>') {
ret += ch;
ch = getchar();
}
return ret;
}
// check for duplicate attributes
bool duplicate(string& str) {
vector<string> attrs;
for (;;) {
while (str.length() > 0 && str[0] == ' ') {
str = str.substr(1);
}
int pos = str.find_first_of('=');
if (pos == string::npos) {
break;
}
attrs.push_back(str.substr(0, pos));
str = str.substr(pos);
while (str.length() > 0 && str[0] != ' ') {
str = str.substr(1);
}
}
sort(attrs.begin(), attrs.end());
for (int i = 0, m = attrs.size(); i < m - 1; ++i) {
if (attrs[i] == attrs[i + 1]) {
return true;
}
}
return false;
}
// read tags, return whether any cases follow
bool readtags(vector<tag>& tags) {
bool valid = true, ret = true;
for (;;) {
string str = tagcontent();
if (str == "?xml version=\"1.0\"?") {
break;
} else if (str == "?end?") {
ret = false;
break;
}
// empty tag
if (str.length() == 0) {
valid = false;
}
tagtype type = begin;
// end tag
if (str[0] == '/') {
type = end;
str = str.substr(1);
}
// stand-alone tag
if (str[str.length() - 1] == '/') {
str = str.substr(0, str.length() - 1);
type = standalone;
}
// parse tag name
int pos = str.find_first_of(' ');
string name = pos == string::npos ? str : str.substr(0, pos);
str = str.substr(name.length());
// check for duplicate attributes
if (duplicate(str)) {
valid = false;
}
tags.push_back(tag(name, type));
}
// clear invalid case
if (!valid) {
tags.clear();
}
return ret;
}
// validate list of tags
bool validtags(vector<tag>& tags) {
// empty list
if (tags.size() == 0) {
return false;
}
if (tags.size() == 1 && tags[0].type == standalone) {
return true;
}
// no document tag
if (tags[0].type != begin ||
tags[tags.size() - 1].type != end ||
tags[0].name != tags[tags.size() - 1].name) {
return false;
}
// stack of tags
vector<string> stack;
stack.push_back(tags[0].name);
for (int i = 1, m = tags.size() - 1; i < m; ++i) {
tag atag = tags[i];
if (atag.type == begin) {
for (int j = 0; j < stack.size(); ++j) {
if (atag.name == stack[j]) {
return false;
}
}
stack.push_back(atag.name);
} else if (atag.type == end) {
if (atag.name != stack[stack.size() - 1]) {
return false;
}
stack.pop_back();
if (stack.size() == 0) {
return false;
}
}
}
return true;
}
bool run() {
vector<tag> tags;
bool ret = readtags(tags);
if (validtags(tags)) {
cout << "well-formed" << endl;
} else {
cout << "non well-formed" << endl;
}
return ret;
}
int main() {
tagcontent();
while (run());
return 0;
}