-
Notifications
You must be signed in to change notification settings - Fork 0
/
uva00122.cpp
91 lines (79 loc) · 1.97 KB
/
uva00122.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
#include <bits/stdc++.h>
#define UNVISITED -1
#define POS_INF 1 << 25
#define NEG_INF -1 << 25
#define _ ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0), cout.precision(15);
using namespace std;
typedef long long int64;
typedef pair<int, int> ii;
bool valid;
int nNodes;
map<int, int> nodes;
int left(int u) {return u << 1;}
int right(int u) {return (u << 1) + 1;}
int parent(int u) {return u >> 1;}
void init(){
nodes.clear();
valid = true;
nNodes = 0;
}
bool process(int val, string &seq){
int idx = 1;
for(int i = 0; i < seq.size() - 1; ++i){ // seq contains the ending ")"
if(seq[i] == 'L') idx = left(idx);
else idx = right(idx);
}
// cout << "val = " << val << " idx = " << idx << endl;
if(nodes.count(idx)) return false;
else{
nodes[idx] = val;
return true;
}
}
bool sanity_check(){
if(!valid) return valid;
int idx, val, p;
for(auto it = nodes.begin(); it != nodes.end(); ++it){
idx = it->first; val = it->second;
p = parent(idx);
if(p != 0 && nodes.count(p) == 0) return false;
}
return true;
}
void execute(){
if(!sanity_check()){
cout << "not complete" << endl;
return;
}
for(auto it = nodes.begin(); it != nodes.end(); ++it){
cout << it->second;
--nNodes;
if(nNodes) cout << " ";
}
cout << endl;
}
int main(){ _
char leftBracket, rightBracket, comma;
int val; string seq;
string input;
init();
while(cin >> input){
if(input == "()"){
execute();
init();
continue;
}
stringstream ss(input);
ss >> leftBracket >> val;
if(ss.bad()){
valid = false;
continue;
} else {
ss >> comma >> seq;
++nNodes;
valid = valid && process(val, seq);
// if(!valid) cout << "invalid since " << input << endl;
}
}
return 0;
}