Cod sursa(job #3196750)

Utilizator Manolea_Teodor_StefanManolea Teodor Stefan Manolea_Teodor_Stefan Data 24 ianuarie 2024 18:35:51
Problema Bool Scor 50
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.54 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin("bool.in");
ofstream fout("bool.out");

/*
 *  Ranking
 *  1. NOT
 *  2. AND
 *  3. OR
 */

vector<bool> varBool;
string expr;

int gIndex;

bool op1();
bool op2();
bool op3();

bool valoareaConstanta(const short& i, const bool& needInverse) {
    bool ret = varBool[i];
    if (needInverse) {
        ret = !ret;
    }
    return ret;
}
bool whiteSpace() {
    return expr[gIndex] == ' ';
}

bool op1() {
    bool ret = op2();
    while (expr[gIndex] == 'O' && expr[gIndex+1] == 'R') {
        gIndex += 2;
        if (whiteSpace()) {
            gIndex++;
        }
        bool mem = op2();
        if (whiteSpace()) {
            gIndex++;
        }
        ret = ret || mem;
    }
    return ret;
}
bool op2() {
    bool ret = op3();
    while (expr[gIndex] == 'A' && expr[gIndex+1] == 'N' && expr[gIndex+2] == 'D') {
        gIndex += 3;
        if (whiteSpace()) {
            gIndex++;
        }
        bool mem = op3();
        if (whiteSpace()) {
            gIndex++;
        }
        ret = ret && mem;
    }
    return ret;
}
bool op3() {
    bool inverse = false;
    if (expr[gIndex] == 'N' && expr[gIndex+1] == 'O' && expr[gIndex+2] == 'T') {
        gIndex += 3;
        inverse = true;
        if (whiteSpace()) {
            gIndex++;
        }
    }

    if (expr[gIndex] == '(') {
        gIndex++;
        bool mem = op1();
        gIndex++;
        if (whiteSpace()) {
            gIndex++;
        }
        if (inverse) {
            mem = !mem;
        }
        return mem;
    }

    if (expr[gIndex] == 'T' && expr[gIndex+1] == 'R') {
        gIndex += 4;
        if (whiteSpace()) {
            gIndex++;
        }
        if (inverse) {
            return false;
        } else {
            return true;
        }
    } else if (expr[gIndex] == 'F' && expr[gIndex+1] == 'A') {
        gIndex += 5;
        if (whiteSpace()) {
            gIndex++;
        }
        if (inverse) {
            return true;
        } else {
            return false;
        }
    } else if ('A' <= expr[gIndex] && expr[gIndex] <= 'Z') {
        bool mem = valoareaConstanta(expr[gIndex], inverse);
        gIndex++;
        if (whiteSpace()) {
            gIndex++;
        }
        return mem;
    }
}

int main() {
    varBool.assign(300, false);
    getline(fin, expr);
    int n;
    fin >> n;
    while (n--) {
        char ch;
        fin >> ch;
        varBool[ch] = !varBool[ch];
        if (op1()) {
            fout << 1;
        } else {
            fout << 0;
        }
        gIndex = 0;
    }
    return 0;
}