Cod sursa(job #3330633)

Utilizator Andrei-Dani-10Pisla Andrei Daniel Andrei-Dani-10 Data 20 decembrie 2025 16:58:26
Problema Bool Scor 70
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.59 kb
#include <fstream>
#include <string>

using namespace std;

ifstream in("bool.in");
ofstream out("bool.out");

const int nmax = 1000;
int n, nrq; string str;

char readstr[nmax + 2];
string linestr, buildstr;

int toggle[nmax + 2];

///NOT = priority 1
///AND = priority 2
///OR  = priority 3

///E = NOT A
///A = O & O ... & O
///O = E / F
///F = 1 / 0 / toggle[char]

int notexpresion(int &idx);
int andexpresion(int &idx);
int orexpresion(int &idx);
int fexpresion(int &idx);

int notexpresion(int &idx){

    if(buildstr[idx] == '~'){
        idx++; return (notexpresion(idx) ^ 1);
    }

    return fexpresion(idx);
}

int andexpresion(int &idx){
    int value = notexpresion(idx);

    for(; idx <= n && buildstr[idx] == '&'; ){
        idx += 1, value &= notexpresion(idx);
    }

    return value;
}

int orexpresion(int &idx){
    int value = andexpresion(idx);

    for(; idx <= n && buildstr[idx] == '|'; ){
        idx += 1, value |= andexpresion(idx);
    }

    return value;
}

int fexpresion(int &idx){
    int value = 0;

    if(buildstr[idx] == '('){
        idx++; value = notexpresion(idx); idx++;
    }else if('A' <= buildstr[idx] && buildstr[idx] <= 'Z'){
        value = toggle[buildstr[idx] - 'A']; idx++;
    }else{
        value = (buildstr[idx] - '0'); idx++;
    }

    return value;
}

int main(){

    in.getline(readstr, nmax);

    linestr.push_back('#');
    for(int idx = 0; readstr[idx]; idx++){
        linestr.push_back(readstr[idx]);
    }; linestr.push_back('#');

    n = linestr.size() - 2;

    string substring3, substring4;
    for(int i = 1; i <= n; i++){
        if(linestr[i] == ' '){ continue; }

        substring3 = linestr.substr(i, 3);

        if(substring3 == "AND"){
            buildstr.push_back('&'); i += 3; ///skip space
        }else if(substring3 == "NOT"){
            buildstr.push_back('~'); i += 3; ///skip space
        }else if(substring3 == "OR "){
            buildstr.push_back('|'); i += 2; ///skip space
        }else if(substring3 == "TRU"){
            buildstr.push_back('1'); i += 4; ///skip space
        }else if(substring3 == "FAL"){
            buildstr.push_back('0'); i += 5; ///skip space
        }else{
            buildstr.push_back(linestr[i]);
        }
    }

    buildstr = '#' + buildstr + '#';
    ///out<<"buildstr: "<<buildstr<<"\n";

    ///From here on we have the simplifed version of the problem

    in>>nrq>>str;

    for(auto f : str){
        toggle[f - 'A'] ^= 1; int pos = 1;
        out<<notexpresion(pos);
    }

    return 0;
}