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>
#define pb push_back
#define x first
#define y second
#define all(a) (a).begin(), (a).end()
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
const int maxn = 1e6 + 5;
int sz, n;
string s;
int type[maxn];
int l[maxn], r[maxn];
int L[maxn], R[maxn];
vector<int> adj[maxn];
void solve(int x) {
if(adj[x].size() == 0) {
l[x] = r[x] = L[x] = R[x] = 1;
return;
}
int y = adj[x][0], z = adj[x][1];
solve(y), solve(z);
L[x] = 1, R[x] = R[y] + R[z];
if(type[x] == 0) {
l[x] = min(l[y], l[z]);
r[x] = r[y] + r[z] - 1;
}
if(type[x] == 1) {
l[x] = l[y] + l[z];
r[x] = max(r[y] + R[z], r[z] + R[y]);
}
}
int main() {
cin >> s;
sz = (int)s.size();
int idx = 0;
vector<int> st;
for(int i = sz - 1;i >= 0;i--) {
if(s[i] == '?') st.pb(idx++);
if(s[i] == 'i' || s[i] == 'a') {
type[idx] = (s[i] == 'a');
adj[idx].pb(st.back()), st.pop_back();
adj[idx].pb(st.back()), st.pop_back();
st.pb(idx++);
}
}
idx--;
solve(idx);
printf("%d\n", r[idx] - l[idx] + 1);
return 0;
}
# | 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... |