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;
map<string , int> comma;
pair<int , int> solve(string s)
{
pair<int ,int> ret = {1e9 , -1e9};
if(s[0] == '?')
{
return {1 , n};
}
int pos = comma[s];
string a = s.substr(4, pos - 4);
string b = s.substr(pos + 1 , (int)s.size() - 2 - pos);
pair<int ,int> va = solve(a) , vb = solve(b);
if(s[1] == 'a')
{
if(va.first +1 <= vb.second)
{
ret.first = min(ret.first , va.first + 1);
ret.second = max(ret.second , vb.second);
}
if(vb.first + 1 <= va.second)
{
ret.first = min(ret.first , vb.first + 1);
ret.second = max(ret.second , va.second);
}
}
else
{
if(vb.first <= va.second - 1)
{
ret.first = min(ret.first , vb.first);
ret.second = max(ret.second , va.second - 1);
}
if(va.first <= vb.second - 1)
{
ret.first = min(ret.first , va.first);
ret.second = max(ret.second , vb.second - 1);
}
}
return ret;
}
int main()
{
string s;
cin>>s;
n = 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 - st.top() + 3;
}
if(s[i] == ')')
{
string t = s.substr(st.top() - 3 , i - st.top() + 4);
comma[t] = pos[st.top()];
st.pop();
}
}
pair<int ,int> final = solve(s);
cout<<final.second - final.first<<'\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... |