Cod sursa(job #3349937)

Utilizator eric_dragosDragos Eric eric_dragos Data 3 aprilie 2026 17:46:59
Problema Bool Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.09 kb
#include <bits/stdc++.h>
#define ll long long
using namespace std;
ifstream fin("bool.in");
ofstream fout("bool.out");

unordered_map<char, bool> mp;

vector<string> divideOperators(string &s){
    int n = s.size();
    vector<string> res;
    for(int i = 0; i<n; i++){
        if(isalnum(s[i]) && (i == 0 || s[i-1] == ' ' || s[i-1] == '(')){
            string token;
            while(i < n && isalnum(s[i])){
                token += s[i++];
            }
            i--;
            res.push_back(token);
        }
        else if(s[i] == ' ') continue;
        else{
            string token = string(1, s[i]);
            res.push_back(token);
        }
    }
    return res;
}
bool isOperator(string c){
    return c == "OR" || c == "NOT" || c == "AND";
}
bool isOperand(string c){
    return !(c == "OR" || c == "NOT" || c == "AND" || c == "(" || c == ")");
}
int precedence(string s){
    if(s == "NOT") return 3;
    else if(s == "AND") return 2;
    else if(s == "OR") return 1;
    return -1;
}
vector<string> infixToPrefix(vector<string> &tokens){
    stack<string> st;
    vector<string> res;
    int n = tokens.size();

    for(int i = n-1; i >=0; i--){
        string s = tokens[i];
        if(isOperand(s)){
            res.push_back(s);
        }
        if(s == ")"){
            st.push(s);
        }
        else if(s == "("){
            while(!st.empty() && st.top() != ")"){
                res.push_back(st.top());
                st.pop();
            }
            if(!st.empty()) st.pop();
        }
        else if(isOperator(s)){
            while(!st.empty() && isOperator(st.top()) &&
                    precedence(s) < precedence(st.top())
                ){
                    res.push_back(st.top());
                    st.pop();
                }
            st.push(s);
        }

    }
        while(!st.empty()){
            res.push_back(st.top());
            st.pop();
        }
    reverse(res.begin(), res.end());
    return res;
}

int evaluateExp(vector<string> &tokens){
    int n = tokens.size();
    stack<bool> st;
    for(int i = n-1; i>=0; i--){
        string s = tokens[i];
        if(s == "TRUE") st.push(1);
        else if(s == "FALSE")st.push(0);
        else if(!isOperator(s)){
            st.push(mp[s[0]]);
        }
        else if(s == "NOT"){
            bool c = st.top();
            st.pop();
            st.push(!c);
        }
        else{
            bool val1 = st.top();st.pop();
            bool val2 = st.top(); st.pop();

            if(s == "AND") st.push(val1 && val2);
            else if(s == "OR") st.push(val1 || val2);
        }
    }
    return st.top();
}

int main(){
    int n;
    string s;
    getline(fin, s);
    for(int i = 0; i<s.size(); i++) mp[s[i]] = 0;
    vector<string> tokens = divideOperators(s);
    vector<string> polish = infixToPrefix(tokens);
    fin >> n;
    char changes[n];
    for(int i = 0; i<n; i++){
        fin >> changes[i];
        mp[changes[i]] = 1-mp[changes[i]];
        fout << evaluateExp(polish);
    }
    return 0;
}