#include <bits/stdc++.h>
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::string s;
std::cin >> s;
if (s[0] != 'm') {
std::cout << "1\n";
return 0;
}
std::vector<std::vector<int>> at_depths;
for (int i = 0, depth = 0; i < s.length(); ++i) {
depth += s[i] == '(';
depth -= s[i] == ')';
if (s[i] == ',') {
if (at_depths.size() <= depth) {
at_depths.resize(depth + 1);
}
at_depths[depth].push_back(i);
}
}
std::vector<std::vector<int>> adj;
std::vector<bool> is_max; // 0 == min, 1 == max
std::vector<int> trans;
int node = 0, leaf = 0;
auto make = [&](auto &&self, int l, int r, int d) -> int {
int cur = node++;
while (adj.size() <= cur) {
adj.push_back({});
is_max.push_back(false);
trans.push_back(0);
}
if (s[l] == '?') {
trans[cur] = leaf++;
return cur;
}
is_max[cur] = s[l + 1] == 'a';
int prev = l + 4;
for (auto it = std::lower_bound(at_depths[d].begin(), at_depths[d].end(), l); it != at_depths[d].end() and *it <= r; ++it) {
int u = self(self, prev, *it - 1, d + 1);
adj[cur].push_back(u);
prev = *it + 1;
}
int u = self(self, prev, r - 1, d + 1);
adj[cur].push_back(u);
return cur;
};
make(make, 0, s.length() - 1, 1);
const int n = std::count(s.begin(), s.end(), '?');
std::vector<int> p(n);
std::iota(p.begin(), p.end(), 0);
std::vector<int> seen(n);
int ans = 0;
do {
auto dfs = [&](auto &&self, int u) {
if (adj[u].empty()) {
return p[trans[u]];
}
int a = self(self, adj[u][0]);
int b = self(self, adj[u][1]);
if (is_max[u]) {
return std::max(a, b);
}
return std::min(a, b);
};
int u = dfs(dfs, 0);
if (!seen[u]) {
ans += 1;
seen[u] = true;
}
} while (std::next_permutation(p.begin(), p.end()));
std::cout << ans << '\n';
}
# | 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... |