Submission #1197851

#TimeUsernameProblemLanguageResultExecution timeMemory
1197851HanksburgerHomework (CEOI22_homework)C++20
100 / 100
166 ms179864 KiB
#include <bits/stdc++.h>
using namespace std;
struct node
{
    node *lc, *rc;
    int type;
};
stack<node*> s;
pair<int, pair<int, int> > f(node *i)
{
    if (!i->type)
        return {1, {1, 1}};
    pair<int, pair<int, int> > ans1=f(i->lc), ans2=f(i->rc);
    if (i->type==1)
        return {ans1.first+ans2.first, {min(ans1.second.first, ans2.second.first), ans1.second.second+ans2.second.second-1}};
    return {ans1.first+ans2.first, {ans1.second.first+ans2.second.first, max(ans1.first+ans2.second.second, ans1.second.second+ans2.first)}};
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    string str;
    cin >> str;
    s.push(new node);
    for (int i=0; i<str.length(); i++)
    {
        if (str[i]=='m')
        {
            if (str[i+1]=='i')
                s.top()->type=1;
            else
                s.top()->type=2;
            s.top()->lc=new node;
            s.push(s.top()->lc);
            i+=3;
        }
        else if (str[i]=='?')
            s.top()->type=0;
        else if (str[i]==')')
            s.pop();
        else
        {
            s.pop();
            s.top()->rc=new node;
            s.push(s.top()->rc);
        }
    }
    pair<int, pair<int, int> > ans=f(s.top());
    cout << ans.second.second-ans.second.first+1;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...