35分求调(自己已经调了很长时间了)
查看原帖
35分求调(自己已经调了很长时间了)
1175043
00001BF52楼主2024/10/20 22:06
#include <iostream>
#include<string>
#include<stack>
using namespace std;
stack <bool> stk_n; stack <char> stk_f;
int sum_and = 0, sum_or = 0,is_short_circuit = -1, is_short_circuit_or=-1;
void runpop() {
    bool lins = stk_n.top(); stk_n.pop();
    bool lins_1 = stk_n.top(); stk_n.pop();
    if (stk_f.top() == '&') {
        stk_n.push(lins & lins_1);
        if (is_short_circuit_or == -1)
            if (lins_1 == 0 && is_short_circuit == -1) sum_and++;
    }
    else {
        stk_n.push(lins_1 | lins);
        if (is_short_circuit_or == -1)
            if (lins_1 == 1 && is_short_circuit == -1) sum_or++;
    }
    stk_f.pop();
}
int main() {
    string s; cin >> s;
    for (int i = 0; i < s.length(); i++) {
        if (s[i] == '0' || s[i] == '1') {
            stk_n.push(s[i] - 48);
            if (!stk_f.empty() && stk_f.top() == '&') {
                runpop(); if (stk_f.size() == is_short_circuit_or&& s[i + 1] != '&') is_short_circuit_or = -1;
            } 
           
        }
        else if ( s[i] == '&') stk_f.push(s[i]);
        else if (s[i] == '|') {
            stk_f.push(s[i]);
            if (stk_n.top() == 1)
                is_short_circuit_or = stk_f.size(); 
        } 
        else if (s[i] == '(') {
            char lins = stk_f.top();
            stk_f.pop();
            while (!stk_f.empty()&& stk_f.top() != '(') runpop();
            if (!stk_f.empty() && is_short_circuit == -1) {
                if (stk_f.top() == '|' && stk_n.top() == 1)
                    is_short_circuit = stk_f.size();
                if (stk_f.top() == '&' && stk_n.top() == 0)
                    is_short_circuit = stk_f.size();
            }
            stk_f.push(lins),stk_f.push(s[i]);
        }
        else {
            while (stk_f.top() != '(') runpop();stk_f.pop();
            if(stk_f.size() == is_short_circuit) is_short_circuit = -1;
            if (stk_f.size() == is_short_circuit_or&&s[i+1]!='&') is_short_circuit_or = -1;
            if (!stk_f.empty() && stk_f.top() == '&')  runpop();
        } 
    }
    while (!stk_f.empty()) runpop();
    cout << stk_n.top() << endl << sum_and << " " << sum_or;
    return 0;
}
2024/10/20 22:06
加载中...