Cod sursa(job #2683520)

Utilizator TheGodFather2131Alexandru Miclea TheGodFather2131 Data 11 decembrie 2020 16:12:38
Problema Bool Scor 30
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.56 kb
//ALEXANDRU MICLEA

#include <vector>
#include <algorithm>
#include <string>
#include <string.h>
#include <cstring>
#include <queue>
#include <map>
#include <set>
#include <unordered_map>
#include <time.h>
#include <iomanip>
#include <deque>
#include <math.h>
#include <cmath>
#include <assert.h>
#include <stack>
#include <bitset>
#include <random>
#include <chrono>
#include <assert.h>

using namespace std;
using ll = long long;

#include <fstream>
//ifstream cin("input.in"); ofstream cout("output.out");
ifstream cin("bool.in"); ofstream cout("bool.out");


//VARIABLES

string s;
bool val[305];

//FUNCTIONS

bool isOp(string c) {
    return c == "OR " || c == "NOT" || c == "AND";
}

int priority(string op) {
    if (op == "NOT") return 3;
    if (op == "AND") return 2;
    if (op == "OR ") return 1;
    return -1;
}

void process_op(stack <bool>& st, string op) { 
    bool r = st.top(); st.pop();
    bool l = st.top(); st.pop();
    
    if (op == "NOT") st.push(l); st.push(!r);
    if (op == "AND") st.push(l && r); 
    if (op == "OR ") st.push(l || r); 

}

bool evaluate() {
    stack <bool> st;
    stack <string> op;

    for (int i = 0; i < (int)s.size(); i++){

        if (s[i] == ' ') continue;

        if (s[i] == '(') op.push("(");
        else if (s[i] == ')') {
            while (op.top() != "(") {
                process_op(st, op.top());
                op.pop();
            }
            op.pop();
        }
       else if (i + 2 < (int)s.size() && isOp(s.substr(i, 3))) {
            string cur_op = s.substr(i, 3);
            while (!op.empty() && priority(op.top()) >= priority(cur_op)) {
                process_op(st, op.top());
                op.pop();
            }
            op.push(cur_op);
            i += 2;
        }
        else if (i + 4 < (int)s.size() && s.substr(i, 5) == "FALSE") {
            st.push(false);
            i += 4;
        }
        else if (i + 3 < (int)s.size() && s.substr(i, 4) == "TRUE") {
            st.push(true);
            i += 3;
        }
        else {
            st.push(val[(int)s[i]]);
        }
    }

    while (!op.empty()) {
        process_op(st, op.top());
        op.pop();
    }

    while (st.size() > 1) st.pop();

    return st.top();
}

//MAIN

int main() {

    getline(cin, s);

    int n; cin >> n;

    for (int i = 1; i <= 300; i++) val[i] = false;

    for (int i = 1; i <= n; i++) {
        char c; cin >> c;
        val[c] ^= 1;
        cout << (int)evaluate();
    }

    return 0;
}