I_NEED_DELP
查看原帖
I_NEED_DELP
658786
STUDENT00楼主2022/10/31 16:44

我,模板栈,样例2直接崩!!!

#include<bits/stdc++.h>
#define one first
#define two second.first
#define three second.second
#define pii pair<bool,pair<int,int> >
using namespace std;
char str[1000010];
stack<pii> st1;
stack<char> st2;
int good(char c){
    if(c=='&') return 2;
    if(c=='|') return 1;
    return 0;
}
void make(){
    pii b=st1.top();
    st1.pop();
    pii a=st1.top();
    st1.pop();
    char c=st2.top();
    st2.pop();
    if(c=='&'){
        if(!a.one) st1.push(make_pair(0,make_pair(1+b.two,a.three+b.three)));
        else st1.push(make_pair(a.one&&b.one,make_pair(a.two+b.two,a.three+b.three)));
    }else{
        if(a.one) st1.push(make_pair(1,make_pair(a.two+b.two,1+b.three)));
        else st1.push(make_pair(a.one||b.one,make_pair(a.two+b.two,a.three+b.three)));
    }
}
int main(){
    scanf("%s",str);
    int len=strlen(str);
    for(int i=0;i<len;i++){
        char c=str[i];
        if(str[i]=='0') st1.push(make_pair(0,make_pair(0,0)));
        else if(str[i]=='1') st1.push(make_pair(1,make_pair(0,0)));
        else if(str[i]=='(') st2.push('(');
        else if(str[i]==')'){
            while(st2.top()!='(') make();
            st2.pop();
        }else{
            while(!st2.empty()&&good(st2.top())>=good(c)) make();
            st2.push(c);
        }
    }
    while(!st2.empty()) make();
    putchar(st1.top().one+48);
    printf("\n%d %d",st1.top().two,st1.top().three);
    return 0;
}
2022/10/31 16:44
加载中...