比赛 CSP2022普及组 评测结果 WWWWEEWEEEEEEEWWWEEE
题目名称 逻辑表达式 最终得分 0
用户昵称 hnzzlza 运行时间 2.585 s
代码语言 C++ 内存使用 10.31 MiB
提交时间 2022-10-29 17:58:38
显示代码纯文本
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. string s;int hz[1000005],tp;
  4. int str[1000005],top,_a,_o,yxj[128];
  5. void instr(char c){
  6. if(c=='0'||c=='1'){hz[tp++]=c-'0';return;}
  7. else {
  8. tp-=2;
  9. if(c=='&'){
  10. if(hz[tp]==0){_a++;hz[tp]=0;}
  11. else hz[tp]=(hz[tp])&(hz[tp+1]);
  12. }
  13. if(c=='|'){
  14. if(hz[tp]==1){_o++;hz[tp]=1;}
  15. else hz[tp]=(hz[tp])|(hz[tp+1]);
  16. }
  17. }
  18. }
  19. void zzh(){
  20. for(int i=0;i<s.size();++i){
  21. if(s[i]=='1'||s[i]=='0')instr(s[i]);
  22. else if(s[i]=='(')str[top++]=s[i];
  23. else if(s[i]=='&'||s[i]=='|'){
  24. if(yxj[str[top]]<yxj[s[i]])str[top++]=s[i];
  25. else {
  26. while(yxj[str[top]]>=yxj[s[i]])instr(char(str[--top]));
  27. str[top++]=s[i];
  28. }
  29. }
  30. else if(s[i]==')'){
  31. while(char(str[top-1])!='('){
  32. instr(char(str[--top]));
  33. }
  34. top--;
  35. }
  36. }
  37. while(top>0)instr(char(str[--top]));
  38. }
  39. int main(){
  40. freopen("csp2022pj_expr.in","r",stdin);
  41. freopen("csp2022pj_expr.out","w",stdout);
  42. yxj['(']=1;yxj['|']=2;yxj['&']=3;
  43. cin>>s;
  44. zzh();
  45. cout<<hz[1]<<endl<<_a<<' '<<_o;
  46. return 0;
  47. }