This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |