1 solutions
-
0
C++ :
#include<iostream> #include<stack> using namespace std; struct node{ int a,b; }s1,s2; struct ed{ int x,y,z; }; stack<ed>End; int cmp(char x){ if(x=='('){ return 0; } if(x=='|'){ return 1; } if(x=='&'){ return 2; } } int head,tail,n; char ans[1000010],s[1000010]; stack<char>opt; stack<node>sum; stack<int> num; int main(){ cin>>s; for(auto now:s){ if(now=='1'||now=='0'){ ans[++tail]=now; }else if(now=='('){ opt.push(now); }else if(now=='&'||now=='|'){ while(!opt.empty()&&cmp(now)<=cmp(opt.top())){ ans[++tail]=opt.top(); opt.pop(); } opt.push(now); }else if(now==')'){ while(opt.top()!='('){ ans[++tail]=opt.top(); opt.pop(); } opt.pop(); } } while(!opt.empty()){ ans[++tail]=opt.top(); opt.pop(); } for(head=1;head<=tail;head++){ if(ans[head]=='&'){ ed y=End.top(); End.pop(); ed x=End.top(); End.pop(); End.push((ed){x.x&y.x,x.y+(x.x==0?1:y.y),x.z+(x.x==0?0:y.z)}); }else if(ans[head]=='|'){ ed y=End.top(); End.pop(); ed x=End.top(); End.pop(); End.push((ed){x.x|y.x,x.y+(x.x==1?0:y.y),x.z+(x.x==1?1:y.z)}); }else{ End.push((ed){ans[head]-'0',0,0}); } } cout<<End.top().x<<"\n"; cout<<End.top().y<<" "<<End.top().z<<"\n"; }
- 1
Information
- ID
- 9132
- Time
- 1000ms
- Memory
- 512MiB
- Difficulty
- (None)
- Tags
- # Submissions
- 0
- Accepted
- 0
- Uploaded By