比赛 |
CSP2022普及组 |
评测结果 |
RRRRRRRRRRRRRRRRRRRR |
题目名称 |
逻辑表达式 |
最终得分 |
0 |
用户昵称 |
zzafanti |
运行时间 |
0.010 s |
代码语言 |
C++ |
内存使用 |
5.74 MiB |
提交时间 |
2022-10-29 16:23:33 |
显示代码纯文本
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int UI;
ll read(){
ll sgn=0,x=0;
char c=getchar();
while(!isdigit(c)) sgn|=(c=='-'),c=getchar();
while(isdigit(c)) x=x*10+c-'0',c=getchar();
return sgn?-x:x;
}
struct res{
bool num;
int c1,c2;
};
string Change(string &str){
string s;
stack<char> stk;
for(int i=0; i<str.size(); i++){
if(isdigit(str[i])){
s+=str[i];
continue;
}
if(str[i]=='('){
stk.push('(');
continue;
}
if(str[i]==')'){
while(stk.size()&&stk.top()!='('){
char c=stk.top();
s+=c;
stk.pop();
}
if(stk.size()) stk.pop();
continue;
}
if(str[i]=='&'){
while(stk.size()&&stk.top()!='('&&stk.top()!='|')
{
s+=stk.top();
stk.pop();
}
stk.push('&');
continue;
}
if(str[i]=='|')
{
while(stk.size()&&stk.top()!='(')
{
s+=stk.top();
stk.pop();
}
stk.push('|');
}
}
while(stk.size()){
s+=stk.top();
stk.pop();
}
return s;
}
void Calu(string &s){
//cout<<s<<endl;
stack<res> stk;
for(int i=0; i<s.size(); i++){
if(isdigit(s[i])){
stk.push((res){s[i]-'0',0,0});
continue;
}
res x,y;
y=stk.top(); stk.pop();
x=stk.top(); stk.pop();
if(s[i]=='&'){
if(!x.num){
stk.push((res){0,x.c1+1,x.c2});
}
else{
stk.push((res){x.num&y.num,x.c1+y.c1,x.c2+y.c2});
}
}
if(s[i]=='|'){
if(x.num){
stk.push((res){1,x.c1,x.c2+1});
}
else{
stk.push(res{x.num|y.num,x.c1+y.c1,x.c2+y.c2});
}
}
}
cout<<stk.top().num<<'\n';
cout<<stk.top().c1<<' '<<stk.top().c2<<'\n';
}
int main(){
string str;
cin>>str;
str=Change(str);
Calu(str);
return 0;
}
/*
1|0&1
*/