-
Notifications
You must be signed in to change notification settings - Fork 1
/
122.cc
165 lines (144 loc) · 3.67 KB
/
122.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
#include <algorithm>
#include <cassert>
#include <cctype>
#include <iostream>
#include <vector>
enum { MAX_NODES = 256, };
struct Node {
std::string path;
int value;
};
struct Tree {
typedef std::vector<Node>::const_iterator const_iterator;
bool IsComplete() const;
const_iterator Find(const_iterator first,
const_iterator end,
const std::string& path) const;
std::vector<Node> nodes;
};
bool operator<(const Node& p, const Node& q);
std::istream& operator>>(std::istream& in, Tree& tree);
std::ostream& operator<<(std::ostream& out, const Tree& tree);
int main() {
Tree tree;
while (std::cin >> tree) {
if (tree.IsComplete()) {
std::cout << tree << std::endl;
} else {
std::cout << "not complete" << std::endl;
}
}
return 0;
}
bool operator<(const Node& p, const Node& q) {
if (p.path.size() != q.path.size()) {
return p.path.size() < q.path.size();
}
for (int i = 0; i < (int)p.path.size(); i++) {
if (p.path[i] != q.path[i]) {
return p.path[i] == 'L';
}
}
return false;
}
bool Tree::IsComplete() const {
// Check if a node is specified more than once.
for (const_iterator i = nodes.cbegin(), j = i + 1; j != nodes.cend();
++i, ++j) {
if (i->path == j->path) {
return false;
}
}
// Special case for root.
if (nodes[0].path != "") {
return false;
}
bool completed[MAX_NODES];
for (int i = 0; i < (int)nodes.size(); i++) {
completed[i] = false;
}
for (const_iterator i = nodes.cend() - 1; i > nodes.cbegin(); --i) {
if (completed[i - nodes.cbegin()]) {
continue;
}
for (const_iterator found, last = i; last != nodes.cbegin(); last = found) {
std::string parent_path(last->path.begin(), last->path.end() - 1);
found = Find(nodes.cbegin(), last, parent_path);
if (found == last) {
return false;
}
completed[found - nodes.cbegin()] = true;
}
}
return true;
}
Tree::const_iterator Tree::Find(const_iterator first,
const_iterator last,
const std::string& path) const {
Node node;
node.path = path;
const_iterator i = std::lower_bound(first, last, node);
return (i != last && i->path == path) ? i : last;
}
std::istream& operator>>(std::istream& in, Tree& tree) {
enum State { STATE_NONE, STATE_VALUE, STATE_COMMA, STATE_PATH, STATE_END, };
tree.nodes.clear();
State state = STATE_NONE;
Node node;
for (char c; in.get(c);) {
switch (state) {
case STATE_NONE:
if (c == '(') {
state = STATE_VALUE;
} else {
assert(isspace(c));
}
break;
case STATE_VALUE:
if (c == ')') {
state = STATE_END;
} else {
assert(isspace(c) || isdigit(c));
in.unget();
in >> node.value;
state = STATE_COMMA;
}
break;
case STATE_COMMA:
if (c == ',') {
state = STATE_PATH;
} else {
assert(isspace(c));
}
break;
case STATE_PATH:
if (c == ')') {
tree.nodes.push_back(node);
node.path.clear();
state = STATE_NONE;
} else if (!isspace(c)) {
node.path.push_back(c);
}
break;
default:
assert(false);
break;
}
if (state == STATE_END) {
break;
}
}
std::sort(tree.nodes.begin(), tree.nodes.end());
return in;
}
std::ostream& operator<<(std::ostream& out, const Tree& tree) {
bool first = true;
for (const auto& node : tree.nodes) {
if (!first) {
out << ' ';
}
first = false;
out << node.value;
}
return out;
}