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;
int n;
vector<int> comma;
string s;
tuple<int , int , int> solve(int l , int r)
{
int mn = 1e9 , mx = -1e9 , sz = 0;
if(s[l] == '?')
{
return {1 , 1 , 1};
}
int pos = comma[l + 3];
auto [am , aM , as] = solve(l + 4 , pos - 4 + l + 4 - 1);
auto [bm , bM , bs] = solve(pos + 1 , r - 1);
sz = as + bs;
if(s[l + 1] == 'a')
{
mn = am + bm;
mx = max(aM + bs , as + bM);
}
else
{
mn = min(am , bm);
mx = aM + bM - 1;
}
return {mn , mx , sz};
}
int main()
{
cin>>s;
n = 0;
comma.assign((int)s.size() , 0);
for(auto c : s)
n+=c=='?';
stack<int> st;
vector<int> pos((int)s.size());
for(int i = 0 ; i < (int)s.size() ; i++)
{
if(s[i] == '(')
st.push(i);
if(s[i] == ',')
{
pos[st.top()] = i;
}
if(s[i] == ')')
{
comma[st.top()] = pos[st.top()];
st.pop();
}
}
auto [a , b , c] = solve(0 , (int)s.size() - 1);
cout<<b - a + 1<<'\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... |