forked from sukhoeing/aoapc-bac2nd-keys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUVa1662.cc
145 lines (130 loc) · 3.38 KB
/
UVa1662.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
// Brackets Removal, NEERC 2005, UVa1662
// 陈锋
#include <cassert>
#include <cctype>
#include <iostream>
#include <functional>
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <set>
#define _for(i,a,b) for( int i=(a); i<(b); ++i)
#define _rep(i,a,b) for( int i=(a); i<=(b); ++i)
using namespace std;
const int MAXN = 1024;
template<typename T>
struct MemPool {
vector<T*> buf;
T* createNew() {
buf.push_back(new T());
return buf.back();
}
void dispose() {
for(int i = 0; i < buf.size(); i++) delete buf[i];
buf.clear();
}
};
struct Node {
char ch;
Node *left, *right;
bool enclose;
int opLevel;
void init(char c){
left = right = NULL; ch = c; enclose = false;
opLevel = 0;
if(ch == '*' || ch == '/') opLevel = 2;
else if(ch == '+' || ch == '-') opLevel = 1;
}
const bool isOp() { return !islower(ch); }
};
string EX;
Node *pRoot;
MemPool<Node> nodePool;
Node* newNode(char c) {
Node* ans = nodePool.createNew();
ans->init(c);
return ans;
}
ostream& operator<<(ostream& os, const Node* p) {
if(!p) return os;
if(p->enclose) os<<'(';
os<<p->left<<p->ch<<p->right;
if(p->enclose) os<<')';
return os;
}
void reverse(Node* p) {
assert(p);
assert(p->isOp());
char c = p->ch;
switch(c) {
case '+' : p->ch = '-'; break;
case '-' : p->ch = '+'; break;
case '*' : p->ch = '/'; break;
case '/' : p->ch = '*'; break;
default:
assert(false);
}
Node *pl = p->left, *pr = p->right;
if(pl && pl->isOp() && pl->opLevel == p->opLevel) reverse(pl);
if(pr && pr->isOp() && !pr->enclose && pr->opLevel == p->opLevel) reverse(pr);
}
void proc(Node* p) {
// cout<<"proc "<<p<<endl;
assert(p);
if(!p->isOp()) return;
Node *pl = p->left, *pr = p->right;
if(pl && pl->isOp()){
if(pl->opLevel >= p->opLevel) pl->enclose = false;
proc(pl);
}
if(pr && pr->isOp()) {
if(pr->opLevel > p->opLevel) pr->enclose = false;
else if(pr->opLevel == p->opLevel) {
if((p->ch=='/' || p->ch == '-') && pr->enclose) {
pr->enclose = false;
reverse(pr);
}
pr->enclose = false;
}
proc(pr);
}
}
Node* parse(int l, int r) {
assert(l <= r);
char lc = EX[l], rc = EX[r];
if(l == r) return newNode(lc);
int p = 0, c1 = -1, c2 = -1;
_rep(i, l, r) {
switch(EX[i]) {
case '(' : p++; break;
case ')' : p--; break;
case '+' : case '-' : if(!p) c1 = i; break;
case '*' : case '/' : if(!p) c2 = i; break;
}
}
if(c1 < 0) c1 = c2;
if(c1 < 0) {
Node* ans = parse(l+1, r-1);
ans->enclose = true;
return ans;
}
Node *ans = newNode(EX[c1]), *ln = ans->left = parse(l, c1-1),
*rn = ans->right = parse(c1+1, r);
assert(ans->opLevel);
if(!ln->isOp()) ln->enclose = false;
if(!rn->isOp()) rn->enclose = false;
return ans;
}
int main(){
while(cin>>EX) {
nodePool.dispose();
pRoot = parse(0, EX.size()-1);
pRoot->enclose = false;
proc(pRoot);
cout<<pRoot<<endl;
}
return 0;
}
// 13761241 1662 Brackets Removal Accepted C++ 0.029 2014-06-17 08:06:24