Cod sursa(job #2922202)

Utilizator PetrescuAlexandru Petrescu Petrescu Data 5 septembrie 2022 22:57:14
Problema Bool Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.26 kb
#include <bits/stdc++.h>

using namespace std;
const int SMAX = 1001, ALPHABET = 26;

bool val[ALPHABET];

void solve(string &s) {
    stack<int> op;
    vector<pair<int, int>> sol;

    int n = s.length();

    for(int i = 0; i < n; i++) {
        while(i < n && s[i] == ' ')
            i++;

        if(i == n)
            break;

        if(s[i] == '(')
           op.push(s[i]);
        else if(s[i] == ')') {
            while(op.top() != '(') {
                sol.push_back({op.top(), 0});
                op.pop();
            }

            op.pop();
        } else {
            string word = "";

            for(; i < n && s[i] != '(' && s[i] != ')' && s[i] != ' '; i++)
                word += s[i];

            //cout << word << '\n';

            if(i < n && s[i] != ' ')
                i--;

            if(word == "NOT") {
                while(!op.empty() && op.top() == 0) {
                    sol.push_back({op.top(), 0});
                    op.pop();
                }

                op.push(0);
            } else if(word == "AND") {
                while(!op.empty() && (op.top() == 0  || op.top() == 1)) {
                    sol.push_back({op.top(), 0});
                    op.pop();
                }

                op.push(1);
            } else if(word == "OR") {
                while(!op.empty() && op.top() != '(') {
                    sol.push_back({op.top(), 0});
                    op.pop();
                }

                op.push(2);
            } else if(word == "FALSE") {
                //cout << "Am pus fals!\n";
                sol.push_back({0, 1});
            }
            else if(word == "TRUE") {
                //cout << "Am pus true!\n";
                sol.push_back({1, 1});
            }
            else {
                //cout << word[0] << '\n';
                sol.push_back({val[word[0] - 'A'], 1});
            }
        }
    }

    while(!op.empty()) {
        sol.push_back({op.top(), 0});
        op.pop();
    }

    /*for(auto x : sol)
        if(x.second == 0) {
            cout << "operatie: ";

            switch(x.first) {
            case 0:
                cout << "NOT\n";
                break;
            case 1:
                cout << "AND\n";
                break;
            case 2:
                cout << "OR\n";
                break;
            }
        } else
            cout << "numar: " << x.first << '\n';*/

    //cout << '\n';
    for(auto x : sol) {
        if(x.second == 0) {
            if(x.first == 0) {
                int a = op.top();
                op.pop();
                op.push(!a);
            } else {
                int b = op.top();
                op.pop();
                int a = op.top();
                op.pop();

                if(x.first == 1)
                    op.push(a & b);
                else
                    op.push(a | b);
            }
        } else
            op.push(x.first);
    }

    cout << op.top();
}

int main()
{
    string s;

    getline(cin, s);

    int n;

    cin >> n;

    string changes;

    cin >> changes;

    for(auto x : changes) {
        val[x - 'A'] = !val[x - 'A'];
        solve(s);
    }

    return 0;
}