-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path753.cpp
More file actions
65 lines (60 loc) · 1.16 KB
/
Copy path753.cpp
File metadata and controls
65 lines (60 loc) · 1.16 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
#include<iostream>
#include<stack>
#include<string>
using namespace std;
struct node {
int start, end, weight;
};
node nodes[1000];
int ori[1000];
void calc(int nodeNum) {
int sum = 1;
if (nodes[nodeNum].start < nodes[nodeNum].end - 1) {
int child = ori[nodes[nodeNum].start + 1];
while (1) {
calc(child);
sum += nodes[child].weight;
if (nodes[child].end == nodes[nodeNum].end - 1) {
break;
}
child = ori[nodes[child].end + 1];
}
}
nodes[nodeNum].weight = sum;
}
int main() {
string str;
while (cin >> str) {
stack<int> stk;
int count = 0;
for (int i = 0; i < str.length();i++) {
if (str[i] == '(') {
stk.push(count);
ori[i] = count;
nodes[count].weight = 0;
nodes[count++].start = i;
}
else {
nodes[stk.top()].end = i;
ori[i] = stk.top();
stk.pop();
}
}
/*
for (int i = 0; i < count; i++) {
cout << nodes[i].start << " " << nodes[i].end << endl;
}
for (int i = 0; i < str.length(); i++) {
cout << ori[i] << " ";
}
cout << endl;
*/
calc(0);
int ans = nodes[0].weight;
for (int i = 1; i < count; i++) {
ans = ans ^ nodes[i].weight;
}
cout << ans << endl;
}
return 0;
}