比赛 |
CSP2022普及组 |
评测结果 |
EEEEEEEEEEEEEEEEEEEE |
题目名称 |
逻辑表达式 |
最终得分 |
0 |
用户昵称 |
flyfreem |
运行时间 |
3.411 s |
代码语言 |
C++ |
内存使用 |
5.74 MiB |
提交时间 |
2022-10-29 16:35:20 |
显示代码纯文本
#include<bits/stdc++.h>
using namespace std;
struct node{
int num,lihuo,liyu;
};
stack <node> a;
stack <char> b;
string n;
int anshuo,ansyu,f;
void yu(){
node s1=a.top();
a.pop();
node s2=a.top();
a.pop();
// cout<<s1.num<<"&"<<s2.num<<endl;
if(s2.num==0){
if(f==1){
a.push((node){0,s2.lihuo,s2.liyu+1});
}else{
a.push((node){0,s2.lihuo+s1.lihuo,s1.liyu+s2.liyu+1});
}
}else if(s1.num==0){
a.push((node){0,s2.lihuo+s1.lihuo,s1.liyu+s2.liyu});
}else{
a.push((node){1,s2.lihuo+s1.lihuo,s1.liyu+s2.liyu});
}
b.pop();
}
void huo(){
node s1=a.top();
a.pop();
node s2=a.top();
a.pop();
// cout<<s1.num<<"|"<<s2.num<<endl;
if(s2.num==1){
if(f==1){
a.push((node){1,s2.lihuo+1,s2.liyu});
}else{
a.push((node){1,s2.lihuo+s1.lihuo+1,s1.liyu+s2.liyu});
}
}else if(s1.num==1){
a.push((node){1,s2.lihuo+s1.lihuo,s1.liyu+s2.liyu});
}else{
a.push((node){0,s2.lihuo+s1.lihuo,s1.liyu+s2.liyu});
}
b.pop();
}
int main(){
freopen("expr.in","r",stdin);
freopen("expr.out","w",stdout);
cin>>n;
for(int i=0;i<n.length();i++){
cout<<i<<endl;
if(n[i]=='1'){
a.push((node){1,0,0});
}else if(n[i]=='0'){
a.push((node){0,0,0});
}else if(n[i]=='('){
b.push('(');
}else if(n[i]=='|'){
if(b.size()&&b.top()=='&')yu();
else if(b.size()&&b.top()=='|')huo();
b.push('|');
}else if(n[i]=='&'){
if(b.size()&&b.top()=='&')yu();
else if(b.size()&&b.top()=='|')huo();
b.push('&');
}else if(n[i]==')'){
while(b.top()!='('&&b.size()&&a.size()){
// cout<<"2\n";
if(b.top()=='*'){
f=1;
b.pop();
}
if(b.top()=='|')huo();
else if(b.top()=='&')yu();
f=0;
}
b.pop();
f=1;
if(b.size()&&b.top()=='&')yu();
else b.push('*');
f=0;
}
}
while(b.size()){
cout<<"2\n";
if(b.size()&&b.top()=='*'){
f=1;
b.pop();
}
if(b.size()&&b.top()=='|')huo();
else if(b.size()&&b.top()=='&')yu();
f=0;
}
cout<<a.top().num<<endl<<a.top().liyu<<" "<<a.top().lihuo;
return 0;
}