比赛 |
CSP2022普及组 |
评测结果 |
WWWWAWWEWWWWWWWWWWEW |
题目名称 |
逻辑表达式 |
最终得分 |
5 |
用户昵称 |
蜀山鸭梨大 |
运行时间 |
0.679 s |
代码语言 |
C++ |
内存使用 |
3.43 MiB |
提交时间 |
2022-10-29 17:24:48 |
显示代码纯文本
#include<bits/stdc++.h>
using namespace std;
int sum1,sum2,l,r;
string str;
stack <int> s1;
stack <char> s2;
int rr(char x){
if(x=='|'){
return 1;
}
else{
return 2;
}
}
int main(){
freopen("csp2022pj_expr.in","r",stdin);
freopen("csp2022pj_expr.out","w",stdout);
cin>>str;
s2.push('(');
str.append(1,')');
for(int i=0;i<str.length();i++){
if(l){
if(str[i]=='('){
l++;
}
if(str[i]==')'){
r++;
}
if(l==r){
l=0,r=0;
}
continue;
}
if(str[i]=='0'||str[i]=='1'){
s1.push(str[i]-'0');
}
else{
if(!s2.size()||s2.top()=='('){
s2.push(str[i]);
continue;
}
if(str[i]=='('){
if(s2.top()=='&'&&s1.top()==0){
s1.pop(),s2.pop(),s1.push(0);
l=1,sum1++;
continue;
}
if(s2.top()=='|'&&s1.top()==1){
s1.pop(),s2.pop(),s1.push(1);
l=1,sum2++;
continue;
}
}
if(str[i]==')'){
while(s2.top()=='('){
if(s2.top()=='|'){
bool a=s1.top();
s1.pop();
bool b=s1.top();
s1.pop();
if(a){
sum2++;
}
s1.push(a|b);
s2.pop();
}
else{
bool a=s1.top();
s1.pop();
bool b=s1.top();
s1.pop();
if(!a){
sum1++;
}
s1.push(a&b);
s2.pop();
s2.push(str[i]);
}
}
s2.pop();
}
if(s2.size()&&s2.top()!='('&&rr(s2.top())>=rr(str[i])){
if(s2.top()=='|'){
bool a=s1.top();
s1.pop();
bool b=s1.top();
s1.pop();
if(a){
sum2++;
}
s1.push(a|b);
s2.pop();
s2.push(str[i]);
}
else{
bool a=s1.top();
s1.pop();
bool b=s1.top();
s1.pop();
if(!a){
sum1++;
}
s1.push(a&b);
s2.pop();
s2.push(str[i]);
}
}
}
}
cout<<s1.top()<<endl<<sum1<<" "<<sum2;
// cout<<endl<<s1.size()<<" "<<s2.size()<<endl;
// while(s1.size()){
// cout<<s1.top()<<" ";
// s1.pop();
// }
// cout<<endl;
// while(s2.size()){
// cout<<s2.top()<<" ";
// s2.pop();
// }
// cout<<endl;
return 0;
}