#include <bits/stdc++.h>
using namespace std;
struct Node
{
bool isLeaf;
bool isMin;
Node* left;
Node* right;
};
string s;
int pos;
Node* parse()
{
if(s[pos]=='?')
{
pos++;
Node* t=new Node();
t->isLeaf=true;
t->left=t->right=nullptr;
return t;
}
if(s.compare(pos,3,"min")==0||s.compare(pos,3,"max")==0)
{
bool isMin=(s[pos]=='m'&&s[pos+1]=='i');
pos+=isMin?3:3;
pos++; // '('
Node* t=new Node();
t->isLeaf=false;
t->isMin=isMin;
t->left=parse();
pos++; // ','
t->right=parse();
pos++; // ')'
return t;
}
return nullptr;
}
pair<int,int> dfs(Node* t,int N)
{
if(t->isLeaf)
{
return {1,N};
}
auto L=dfs(t->left,N);
auto R=dfs(t->right,N);
if(t->isMin)
{
int l=L.first+R.first-1;
int r=min(L.second,R.second);
l=max(l,1);
r=min(r,N);
if(l>r) l=r+1;
return {l,r};
}
else
{
int l=max(L.first,R.first);
int r=L.second+R.second-1;
l=max(l,1);
r=min(r,N);
if(l>r) l=r+1;
return {l,r};
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin>>s;
int N=0;
for(char c:s) if(c=='?') N++;
pos=0;
Node* root=parse();
auto res=dfs(root,N);
cout<<res.second-res.first+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... |