1 solutions

  • 0
    @ 2025-11-5 18:26:21

    C++ :

    #include<bits/stdc++.h>
    using namespace std;
    string s;
    int n,p[1000005];
    stack <int> st;
    struct Result{
      int ans,x,y;//x and y or
    };
    Result calc(Result l,char op,Result r)
    {
      Result res;
      if(op=='|')
      {
        if(l.ans) res=l, res.y++;
        else res=Result({r.ans,l.x+r.x,l.y+r.y});
      }
      else{
        if(!l.ans) res=l,res.x++;
        else res=Result({r.ans,l.x+r.x,l.y+r.y});
      }
      return res;
    }
    
    Result solve(int st,int ed)
    {
      
      if(p[st]&&p[st]==ed) return solve(st+1,ed-1);
      Result v, cur({1,0,0}), res({0,0,0});
      for(int i=st;i<=ed;i++)
      {
        if(p[i]){
          v=solve(i+1,p[i]-1);
          i=p[i]+1;
        }
        else{
          v=Result({s[i]-'0',0,0});
          i++;
        }
        cur=calc(cur,'&',v);
        if(i>ed||s[i]=='|')
        {
          res=calc(res,'|',cur);
          cur=Result({1,0,0});
        }
      }
      return res;
    }
    int main()
    {
      //  freopen("expr.in", "r", stdin);
      //  freopen("expr.out", "w", stdout);
      cin>>s;
      n=s.length();
      for(int i=0;i<n;i++)
      {
        if(s[i]=='(') st.push(i);
        else if(s[i]==')')
        {
          p[st.top()]=i;
          st.pop();
        }
      }
      Result res=solve(0,n-1);
      cout<<res.ans<<endl;
      cout<<res.x<<" "<<res.y; 
     return 0;
    }
    
    • 1

    Information

    ID
    18877
    Time
    2000ms
    Memory
    512MiB
    Difficulty
    (None)
    Tags
    # Submissions
    0
    Accepted
    0
    Uploaded By