#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
string s;
cin >> s;
int M = 0;
vector<int> l, r, q, leaf;
auto rec = [&](auto rec, int a, int b) -> int {
if (a + 1 == b) {
l.push_back(-1);
r.push_back(-1);
q.push_back(-1);
leaf.push_back(M);
return M++;
}
int c = 0, z = -1;
for (int i = a; i < b; i++) {
if (s[i] == '(') {
c++;
} else if (s[i] == ')') {
c--;
} else if (s[i] == ',') {
if (c == 1) {
z = i;
break;
}
}
}
int u = rec(rec, a + 4, z);
int v = rec(rec, z + 1, b - 1);
l.push_back(u);
r.push_back(v);
q.push_back(s[a + 1] == 'a' ? 1 : 0);
return M++;
};
rec(rec, 0, s.size());
int N = leaf.size();
if (N <= 16) {
vector<vector<int>> T(N + 1);
vector<vector<int>> S(1 << N);
for (int i = 0; i < (1 << N); i++) {
T[__builtin_popcount(i)].push_back(i);
for (int j = 0; j < N; j++) {
if (i >> j & 1) {
S[i].push_back(j);
}
}
}
vector<int> sz(M);
auto dfs = [&](auto dfs, int x) -> vector<vector<bool>> {
vector f(1 << N, vector<bool>(N));
if (l[x] == -1) {
sz[x] = 1;
for (int i = 0; i < N; i++) {
f[1 << i][i] = 1;
}
return f;
};
auto u = dfs(dfs, l[x]);
auto v = dfs(dfs, r[x]);
sz[x] += sz[l[x]];
sz[x] += sz[r[x]];
for (int a : T[sz[l[x]]]) {
for (int i : S[a]) {
if (!u[a][i]) {
continue;
}
for (int b : T[sz[r[x]]]) {
if (a & b) {
continue;
}
for (int j : S[b]) {
if (!v[b][j]) {
continue;
}
f[a + b][q[x] == 0 ? min(i, j) : max(i, j)] = 1;
}
}
}
}
return f;
};
auto f = dfs(dfs, M - 1);
int ans = 0;
for (int i = 0; i < N; i++) {
if (f[(1 << N) - 1][i]) {
ans++;
}
}
cout << ans << "\n";
return 0;
}
vector<int> a;
int x = M - 1;
while (l[x] != -1) {
a.push_back(q[x]);
if (l[l[x]] != -1) {
x = l[x];
} else {
x = r[x];
}
}
for (int i = 1; i < N - 1; i++) {
if (a[i] != a[i - 1]) {
cout << N - i << "\n";
return 0;
}
}
cout << 1 << "\n";
return 6/22;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
596 KB |
Output is correct |
2 |
Correct |
1 ms |
596 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
1 ms |
584 KB |
Output is correct |
5 |
Correct |
1 ms |
608 KB |
Output is correct |
6 |
Correct |
1 ms |
580 KB |
Output is correct |
7 |
Correct |
1 ms |
576 KB |
Output is correct |
8 |
Correct |
1 ms |
596 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
700 KB |
Output is correct |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
15 |
Correct |
1 ms |
724 KB |
Output is correct |
16 |
Correct |
2 ms |
724 KB |
Output is correct |
17 |
Correct |
1 ms |
852 KB |
Output is correct |
18 |
Correct |
1 ms |
852 KB |
Output is correct |
19 |
Correct |
1 ms |
828 KB |
Output is correct |
20 |
Correct |
1 ms |
724 KB |
Output is correct |
21 |
Correct |
1 ms |
724 KB |
Output is correct |
22 |
Correct |
1 ms |
596 KB |
Output is correct |
23 |
Correct |
1 ms |
724 KB |
Output is correct |
24 |
Correct |
1 ms |
572 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
596 KB |
Output is correct |
2 |
Correct |
1 ms |
596 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
1 ms |
584 KB |
Output is correct |
5 |
Correct |
1 ms |
608 KB |
Output is correct |
6 |
Correct |
1 ms |
580 KB |
Output is correct |
7 |
Correct |
1 ms |
576 KB |
Output is correct |
8 |
Correct |
1 ms |
596 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
700 KB |
Output is correct |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
15 |
Correct |
1 ms |
724 KB |
Output is correct |
16 |
Correct |
2 ms |
724 KB |
Output is correct |
17 |
Correct |
1 ms |
852 KB |
Output is correct |
18 |
Correct |
1 ms |
852 KB |
Output is correct |
19 |
Correct |
1 ms |
828 KB |
Output is correct |
20 |
Correct |
1 ms |
724 KB |
Output is correct |
21 |
Correct |
1 ms |
724 KB |
Output is correct |
22 |
Correct |
1 ms |
596 KB |
Output is correct |
23 |
Correct |
1 ms |
724 KB |
Output is correct |
24 |
Correct |
1 ms |
572 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
505 ms |
52184 KB |
Output is correct |
30 |
Correct |
501 ms |
52212 KB |
Output is correct |
31 |
Correct |
413 ms |
52256 KB |
Output is correct |
32 |
Correct |
288 ms |
56728 KB |
Output is correct |
33 |
Correct |
487 ms |
47560 KB |
Output is correct |
34 |
Correct |
275 ms |
79892 KB |
Output is correct |
35 |
Correct |
212 ms |
75264 KB |
Output is correct |
36 |
Correct |
179 ms |
66104 KB |
Output is correct |
37 |
Correct |
191 ms |
52188 KB |
Output is correct |
38 |
Correct |
234 ms |
56804 KB |
Output is correct |
39 |
Correct |
376 ms |
70604 KB |
Output is correct |
40 |
Correct |
272 ms |
75192 KB |
Output is correct |
41 |
Correct |
195 ms |
56808 KB |
Output is correct |
42 |
Correct |
220 ms |
61416 KB |
Output is correct |
43 |
Correct |
314 ms |
61312 KB |
Output is correct |
44 |
Correct |
177 ms |
121424 KB |
Output is correct |
45 |
Correct |
174 ms |
107636 KB |
Output is correct |
46 |
Correct |
162 ms |
107648 KB |
Output is correct |
47 |
Correct |
184 ms |
116876 KB |
Output is correct |
48 |
Correct |
183 ms |
126204 KB |
Output is correct |
49 |
Correct |
171 ms |
65956 KB |
Output is correct |
50 |
Correct |
217 ms |
75268 KB |
Output is correct |
51 |
Correct |
423 ms |
56692 KB |
Output is correct |
52 |
Correct |
538 ms |
52132 KB |
Output is correct |
53 |
Correct |
474 ms |
47572 KB |
Output is correct |
54 |
Correct |
227 ms |
47536 KB |
Output is correct |
55 |
Correct |
411 ms |
47692 KB |
Output is correct |
56 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1067 ms |
8924 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
596 KB |
Output is correct |
2 |
Correct |
1 ms |
596 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
1 ms |
584 KB |
Output is correct |
5 |
Correct |
1 ms |
608 KB |
Output is correct |
6 |
Correct |
1 ms |
580 KB |
Output is correct |
7 |
Correct |
1 ms |
576 KB |
Output is correct |
8 |
Correct |
1 ms |
596 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
700 KB |
Output is correct |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
15 |
Correct |
1 ms |
724 KB |
Output is correct |
16 |
Correct |
2 ms |
724 KB |
Output is correct |
17 |
Correct |
1 ms |
852 KB |
Output is correct |
18 |
Correct |
1 ms |
852 KB |
Output is correct |
19 |
Correct |
1 ms |
828 KB |
Output is correct |
20 |
Correct |
1 ms |
724 KB |
Output is correct |
21 |
Correct |
1 ms |
724 KB |
Output is correct |
22 |
Correct |
1 ms |
596 KB |
Output is correct |
23 |
Correct |
1 ms |
724 KB |
Output is correct |
24 |
Correct |
1 ms |
572 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
505 ms |
52184 KB |
Output is correct |
30 |
Correct |
501 ms |
52212 KB |
Output is correct |
31 |
Correct |
413 ms |
52256 KB |
Output is correct |
32 |
Correct |
288 ms |
56728 KB |
Output is correct |
33 |
Correct |
487 ms |
47560 KB |
Output is correct |
34 |
Correct |
275 ms |
79892 KB |
Output is correct |
35 |
Correct |
212 ms |
75264 KB |
Output is correct |
36 |
Correct |
179 ms |
66104 KB |
Output is correct |
37 |
Correct |
191 ms |
52188 KB |
Output is correct |
38 |
Correct |
234 ms |
56804 KB |
Output is correct |
39 |
Correct |
376 ms |
70604 KB |
Output is correct |
40 |
Correct |
272 ms |
75192 KB |
Output is correct |
41 |
Correct |
195 ms |
56808 KB |
Output is correct |
42 |
Correct |
220 ms |
61416 KB |
Output is correct |
43 |
Correct |
314 ms |
61312 KB |
Output is correct |
44 |
Correct |
177 ms |
121424 KB |
Output is correct |
45 |
Correct |
174 ms |
107636 KB |
Output is correct |
46 |
Correct |
162 ms |
107648 KB |
Output is correct |
47 |
Correct |
184 ms |
116876 KB |
Output is correct |
48 |
Correct |
183 ms |
126204 KB |
Output is correct |
49 |
Correct |
171 ms |
65956 KB |
Output is correct |
50 |
Correct |
217 ms |
75268 KB |
Output is correct |
51 |
Correct |
423 ms |
56692 KB |
Output is correct |
52 |
Correct |
538 ms |
52132 KB |
Output is correct |
53 |
Correct |
474 ms |
47572 KB |
Output is correct |
54 |
Correct |
227 ms |
47536 KB |
Output is correct |
55 |
Correct |
411 ms |
47692 KB |
Output is correct |
56 |
Correct |
1 ms |
212 KB |
Output is correct |
57 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
58 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
596 KB |
Output is correct |
2 |
Correct |
1 ms |
596 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
1 ms |
584 KB |
Output is correct |
5 |
Correct |
1 ms |
608 KB |
Output is correct |
6 |
Correct |
1 ms |
580 KB |
Output is correct |
7 |
Correct |
1 ms |
576 KB |
Output is correct |
8 |
Correct |
1 ms |
596 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
700 KB |
Output is correct |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
15 |
Correct |
1 ms |
724 KB |
Output is correct |
16 |
Correct |
2 ms |
724 KB |
Output is correct |
17 |
Correct |
1 ms |
852 KB |
Output is correct |
18 |
Correct |
1 ms |
852 KB |
Output is correct |
19 |
Correct |
1 ms |
828 KB |
Output is correct |
20 |
Correct |
1 ms |
724 KB |
Output is correct |
21 |
Correct |
1 ms |
724 KB |
Output is correct |
22 |
Correct |
1 ms |
596 KB |
Output is correct |
23 |
Correct |
1 ms |
724 KB |
Output is correct |
24 |
Correct |
1 ms |
572 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
505 ms |
52184 KB |
Output is correct |
30 |
Correct |
501 ms |
52212 KB |
Output is correct |
31 |
Correct |
413 ms |
52256 KB |
Output is correct |
32 |
Correct |
288 ms |
56728 KB |
Output is correct |
33 |
Correct |
487 ms |
47560 KB |
Output is correct |
34 |
Correct |
275 ms |
79892 KB |
Output is correct |
35 |
Correct |
212 ms |
75264 KB |
Output is correct |
36 |
Correct |
179 ms |
66104 KB |
Output is correct |
37 |
Correct |
191 ms |
52188 KB |
Output is correct |
38 |
Correct |
234 ms |
56804 KB |
Output is correct |
39 |
Correct |
376 ms |
70604 KB |
Output is correct |
40 |
Correct |
272 ms |
75192 KB |
Output is correct |
41 |
Correct |
195 ms |
56808 KB |
Output is correct |
42 |
Correct |
220 ms |
61416 KB |
Output is correct |
43 |
Correct |
314 ms |
61312 KB |
Output is correct |
44 |
Correct |
177 ms |
121424 KB |
Output is correct |
45 |
Correct |
174 ms |
107636 KB |
Output is correct |
46 |
Correct |
162 ms |
107648 KB |
Output is correct |
47 |
Correct |
184 ms |
116876 KB |
Output is correct |
48 |
Correct |
183 ms |
126204 KB |
Output is correct |
49 |
Correct |
171 ms |
65956 KB |
Output is correct |
50 |
Correct |
217 ms |
75268 KB |
Output is correct |
51 |
Correct |
423 ms |
56692 KB |
Output is correct |
52 |
Correct |
538 ms |
52132 KB |
Output is correct |
53 |
Correct |
474 ms |
47572 KB |
Output is correct |
54 |
Correct |
227 ms |
47536 KB |
Output is correct |
55 |
Correct |
411 ms |
47692 KB |
Output is correct |
56 |
Correct |
1 ms |
212 KB |
Output is correct |
57 |
Execution timed out |
1067 ms |
8924 KB |
Time limit exceeded |
58 |
Halted |
0 ms |
0 KB |
- |