Cod sursa(job #2693481)

Utilizator gasparrobert95Gaspar Robert Andrei gasparrobert95 Data 6 ianuarie 2021 08:45:08
Problema Bool Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.51 kb
#include <bits/stdc++.h>
#define ll long long
using namespace std;
ifstream fin("bool.in");
ofstream fout("bool.out");
bool val[130];
string s, ins;
int pos, n;

void readInputAndCompress() {
    getline(fin, ins);
    for (int i = 0; i < ins.size(); ++i) {
        if (ins.substr(i, 2) == "OR")
            s += 'o', ++i;
        else if (ins.substr(i, 3) == "NOT")
            s += '!', i += 2;
        else if (ins.substr(i, 3) == "AND")
            s += 'a', i += 2;
        else if (ins.substr(i, 4) == "TRUE")
            s += '1', i += 3;
        else if (ins.substr(i, 5) == "FALSE")
            s += '0', i += 4;
        else if (ins[i] != ' ')
            s += ins[i];
    }
    n = s.size() - 1;
    val['1'] = 1;
    return;
}

// Aa((Bo!C)o((1)))
bool _and();
bool termen();

bool _or() {
    bool rez = _and();
    while (s[pos] == 'o')
        ++pos, rez |= _and();
    return rez;
}

bool _and() {
    bool rez = termen();
    while (s[pos] == 'a')
        ++pos, rez &= termen();
    return rez;
}

bool termen() {
    bool rez, _not = false;
    while (s[pos] == '!')
        _not = !_not, ++pos;
    if (s[pos] == '(')
        ++pos, rez = _or();
    else
        rez = val[s[pos]];
    ++pos;
    if (_not)
        rez = !rez;
    return rez;
}

int main() {
    readInputAndCompress();
    int q;
    fin >> q;
    while (q--) {
        char el;
        fin >> el;
        val[el] = !val[el];
        pos = 0;
        fout << _or();
    }
    return 0;
}