-
Notifications
You must be signed in to change notification settings - Fork 0
/
dyckparser.cpp
142 lines (122 loc) · 3.66 KB
/
dyckparser.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
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
#include <iostream>
#include <iomanip>
#include <iterator>
#include <string>
#include <vector>
#include <algorithm>
int main (int argc, char *argv[]) {
std::string input;
std::vector<int> scan_node;
std::vector<int> scan_depth;
std::vector<int> prefix_node;
std::vector<int> prefix_depth;
std::vector<int> depth;
std::vector<int> bfs;
std::vector<int> parent;
std::vector<int> count;
std::vector<int> nchild;
std::vector<int> alloc;
std::vector<int> output;
/* input from stdin */
std::cin >> input;
/* scan input and create preliminary node and depth */
for (int i = 0; i < input.length(); i++) {
if (input[i] == '[') {
scan_node.push_back(1);
scan_depth.push_back(1);
}
else {
scan_node.push_back(0);
scan_depth.push_back(-1);
}
}
/* prefix sum */
prefix_node.push_back(0);
prefix_depth.push_back(0);
for (int i = 0; i < scan_node.size() - 1; i++) {
prefix_node.push_back(scan_node[i] + prefix_node[i]);
prefix_depth.push_back(scan_depth[i] + prefix_depth[i]);
}
/* not balanced. warn and exit */
if (prefix_depth[prefix_depth.size() - 1] != 1) {
std::cout << "This is not a balanced string! Try again.\n";
return -1;
}
/* set size and initialize arrays */
int num_nodes = prefix_node[prefix_node.size() - 1];
depth.resize(num_nodes);
bfs.resize(num_nodes);
parent.resize(num_nodes, 0);
count.resize(num_nodes, 0);
nchild.resize(num_nodes, 0);
alloc.resize(num_nodes, 0);
/* reduce to nodes */
for (int i = 0; i < prefix_depth.size() - 1; i++) {
depth[prefix_node[i]] = prefix_depth[i];
}
/* sort by depth */
int index = 0;
int max_depth = *std::max_element(std::begin(depth), std::end(depth));
for (int i = 0; i <= max_depth; i++) {
for (int j = 0; j < num_nodes; j++) {
if (depth[j] == i) {
bfs[j] = index++;
}
}
}
/* calculate parent */
for (int i = 0; i < num_nodes - 1; i++) {
if (depth[i + 1] == (depth[i] + 1)) {
parent[bfs[i + 1]] = bfs[i];
}
}
/* propagate to the right */
int current_value = parent[0];
for (int i = 0; i < num_nodes; i++) {
if (parent[i] <= current_value) {
parent[i] = current_value;
}
else {
current_value = parent[i];
}
}
/* calculate count and nchild array */
for (int i = 0; i < num_nodes; i++) {
if (parent[i] != parent[i + 1]) {
count[i + 1] = 1;
nchild[parent[i]] = count[i];
}
else {
count[i + 1] = count[i] + 1;
}
}
/* special case for tree with highest depth 1 */
if (parent[parent.size() - 1] == 0) {
nchild[0] = count[count.size() - 1];
}
/* calculate alloc */
for (int i = 0; i < num_nodes; i++) {
alloc[i + 1] = alloc[i] + nchild[i] + 1;
}
/* generate final output */
int output_length = alloc[num_nodes - 1] + 1;
output.resize(output_length);
for (int i = 0; i < num_nodes; i++) {
output[alloc[i]] = nchild[i];
}
/* generate references to children */
for (int i = 1; i < num_nodes; i++) {
output[parent[i] + i] = alloc[i];
}
std::cout << "Index: ";
for (int i = 0; i < output_length; i++) {
std::cout << std::setw(2) << i << " ";
}
std::cout << "\n";
std::cout << "Value: ";
for (auto elem : output) {
std::cout << std::setw(2) << elem << " ";
}
std::cout << "\n";
return 0;
}