forked from chinoxyz/Spoj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ACPC10C.cpp
213 lines (179 loc) · 3.54 KB
/
ACPC10C.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include <cstdio>
#include <algorithm>
#include<iostream>
#include<vector>
#include<climits>
#include <complex>
#include <iostream>
#include <valarray>
#include<cstring>
#include<queue>
#include<bitset>
#include<map>
#include<set>
#include<sstream>
#include<ctime>
#include <stack>
#include <iomanip>
#include <cassert>
#include<cmath>
using namespace std;
#define PB push_back
#define i64 long long
#define FOR(i,a,b) for(i=(a);i<(b);++i)
#define FORE(i,a,b) for(i=(a);i<=(b);++i)
#define REP(i,n) FOR(i,0,n)
#define REPE(i,n) FORE(i,0,n)
#define SZ(v) ((v).size())
#define LD long double
#define VI vector<int>
#define VS vector<string>
#define VD vector<double>
#define VLD vector<LD>
#define MSET(x,y) memset((x),(y),sizeof(x))
#define SZOF(x) sizeof((x))
#define VVI vector< VI >
#define PII pair< int,int >
#define VPII vector< PII >
#define MP make_pair
#define PIPII pair<int, PII >
#define PIPIPII pair< int, PIPII >
#define u64 unsigned i64
#define Vi64 vector<i64>
#define Vu64 vector<u64>
#define MAXN 32008
char arr[MAXN];
int n;
int visd[MAXN];
int md;
int vd[MAXN];
int nodes[MAXN];
VI child[MAXN];
int mp;
int depth[MAXN];
int solve(int l,int r,int dp)
{
int cn=mp;
depth[cn]=dp;
md=max(dp,md);
if(l==r)
{
if(arr[l]=='T')
{
nodes[mp]=1;
}
else
{
nodes[mp]=0;
}
++mp;
return (mp-1);
}
int ll=l+1;
int ct=0;
++mp;
int ti;
int ti2;
while(ll<=r)
{
if(arr[ll]=='(')
{
ti=ll;
++ll;
ct=1;
while(ct && ll<=r)
{
if(arr[ll]=='(')
{
++ct;
}
else if(arr[ll]==')')
{
--ct;
}
++ll;
}
--ll;
ti2=solve(ti,ll,dp+1);
child[cn].PB(ti2);
}
else if(arr[ll]!=')')
{
ti2=solve(ll,ll,dp+1);
child[cn].PB(ti2);
}
++ll;
}
return cn;
}
int main()
{
freopen("Text/ACPC10C.txt","r",stdin);
int cases=0;
int i;
int j,ti,v;
while(true)
{
//MSET(visd,0);
//MSET(vd,0);
md=0;
//MSET(nodes,0);
//MSET(depth,0);
mp=0;
++cases;
scanf("%s",arr+1);
if(strcmp(arr+1,"()\0")==0)
{
break;
}
n=strlen(arr+1);
arr[0]='(';
arr[n+1]=')';
arr[n+2]='\0';
n+=2;
//printf("%s\n",arr);
for(i=0;i<n;++i)
{
child[i].clear();
}
solve(0,n-1,0);
ti=0;
for(i=md-1;i>=0;--i)
{
vd[i]=ti;
ti^=1;
}
for(i=mp-1;i>=0;--i)
{
if(child[i].size())
{
v=nodes[child[i][0]];
if(vd[depth[i]])
{
for(j=1;j<child[i].size();++j)
{
v|=nodes[child[i][j]];
}
}
else
{
for(j=1;j<child[i].size();++j)
{
v&=nodes[child[i][j]];
}
}
nodes[i]=v;
}
}
printf("%d. ",cases);
if(nodes[0])
{
printf("true\n");
}
else
{
printf("false\n");
}
}
return 0;
}