-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy path1911.cc
174 lines (144 loc) · 4.33 KB
/
1911.cc
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
//Name: A Well-Formed Problem
//Level: 2
//Category: 構文解析,やるだけ,強実装
//Note:
/*
* やるだけ.文字列はすべてすっ飛ばして,タグだけ見ていけばよい.
*
* 問題文中のwell-formed条件はサンプルがだいたい網羅している.
* サンプルにない注意点としては,
* ・空タグ(<hoge />)
* ・ルートに空タグ複数出てくる
* ・値のないattribute
* など.
*/
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cctype>
#include <set>
using namespace std;
typedef string::iterator Iterator;
enum Type { TAG_OPEN, TAG_CLOSE, TAG_ALONE };
void debug_print(Iterator it, const Iterator &end) {
while(it != end) cout << *it++;
cout << endl;
}
bool check(Iterator &it, const Iterator &end, char c) {
return (it != end && *it == c);
}
void skip_until(Iterator &it, const Iterator &end, char c, bool end_is_error) {
while(it != end && *it != c) ++it;
if(it == end && end_is_error) throw "Unexpected end";
}
void skip_white(Iterator &it, const Iterator &end) {
while(it != end && isspace(*it)) ++it;
}
void skip(Iterator &it, const Iterator &end, char c) {
if(it == end) throw "Unexpected end";
if(*it != c) throw "Unexpected character";
++it;
skip_white(it, end);
}
string name(Iterator &it, const Iterator &end) {
string str = "";
skip_white(it, end);
while(it != end && (isalnum(*it) || *it == '-')) str += *it++;
skip_white(it, end);
return str;
}
pair<string,Type> tag(Iterator &it, const Iterator &end) {
Type type = TAG_OPEN;
skip(it, end, '<');
if(check(it, end, '/')) { // 閉じタグの判別
skip(it, end, '/');
type = TAG_CLOSE;
}
string tagname = name(it, end);
if(type == TAG_OPEN) {
set<string> attrs;
while(!check(it, end, '/') && !check(it, end, '>')) {
string attrname = name(it, end);
// 4. attributeはダブらない
if(attrs.count(attrname)) throw "Duplicated attribute";
// 6. attributeは値を持つ
skip(it, end, '=');
skip(it, end, '"');
skip_until(it, end, '"', true);
skip(it, end, '"');
attrs.insert(attrname);
}
if(check(it, end, '/')) {
skip(it, end, '/');
type = TAG_ALONE;
}
}
skip(it, end, '>');
return make_pair(tagname, type);
}
void parse(Iterator &it, const Iterator &end) {
vector<string> stk;
bool first = true;
skip_white(it, end);
while(check(it, end, '<')) {
pair<string,Type> t = tag(it, end);
//cout << t.first << endl;
if(stk.size() == 0 && !first) {
// 1. ルート要素は1つでなければならない
throw "Too many roots";
}
if(t.second != TAG_CLOSE && find(stk.begin(), stk.end(), t.first) != stk.end()) {
// 5. 同名タグは入れ子にならない
throw "Recursive reference";
}
if(t.second == TAG_OPEN) {
stk.push_back(t.first);
first = false;
}
else if(t.second == TAG_CLOSE) {
if(stk.size() == 0 || stk.back() != t.first) {
// 2,3. タグは正常にネストしないといけない
throw "Unmatched tag";
}
stk.pop_back();
}
skip_until(it, end, '<', false);
}
// 2. タグは必ず閉じなければならない
if(stk.size() > 0) throw "Unclosed tag";
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
{
// Skip first header
string line;
getline(cin, line);
}
bool end = false;
while(!end) {
string txt;
while(true) {
string line;
getline(cin, line);
if(line == "<?end?>") {
end = true;
break;
}
if(line == "<?xml version=\"1.0\"?>") break;
txt += " ";
txt += line;
}
bool well_formed = true;
try {
Iterator it = txt.begin();
parse(it, txt.end());
} catch(const char *str) {
//cout << str << endl;
well_formed = false;
}
cout << (well_formed?"" : "non ") << "well-formed" << endl;
}
return 0;
}