#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 |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
332 ms |
524288 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |