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, N = 0;
vector<int> l, r, q, stk, t(s.size());
for (int i = 0; i < s.size(); i++) {
if (isalpha(s[i])) {
continue;
}
if (s[i] == '?') {
l.push_back(-1);
r.push_back(-1);
q.push_back(-1);
N++;
t[i] = M++;
} else if (s[i] == '(') {
stk.push_back(i);
} else if (s[i] == ',') {
int j = stk.back();
t[j] = t[i - 1];
} else if (s[i] == ')') {
int j = stk.back();
stk.pop_back();
l.push_back(t[j]);
r.push_back(t[i - 1]);
q.push_back(s[j - 1] == 'x' ? 1 : 0);
t[i] = M++;
}
}
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 (1) {
a.push_back(q[x]);
if (l[l[x]] != -1) {
x = l[x];
} else if (l[r[x]] != -1) {
x = r[x];
} else {
break;
}
}
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;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:11:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
11 | for (int i = 0; i < s.size(); i++) {
| ~~^~~~~~~~~~
# | 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... |