Cod sursa(job #3326596)

Utilizator adelina_15InfoAdelina Radoi adelina_15Info Data 29 noiembrie 2025 15:34:35
Problema Bool Scor 70
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.14 kb
#include <bits/stdc++.h>

using namespace std;

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

int n;
string s, sini;
map<char, bool> val;

void TransS()
{
    n = sini.size();
    for(int i = 0; i < n; i++)
    {
        if(sini[i] == 'A' && (i < n-1 && sini[i+1] == 'N'))
        {
            s += '&';
            i += 2;
        }
        else if(sini[i] == 'O' && (i < n-1 && sini[i+1] == 'R'))
        {
            s += '|';
            i++;
        }
        else if(sini[i] == 'N' && (i < n-1 && sini[i+1] == 'O'))
        {
            s += '!';
            i+=2;
        }
        else if(sini[i] == 'T' && (i < n-1 && sini[i+1] == 'R'))
        {
            s += '1';
            i+=3;
        }
        else if(sini[i] == 'F' && (i < n-1 && sini[i+1] == 'A'))
        {
            s += '0';
            i+=3;
        }
        else
        {
            if(sini[i] >= 'A' && sini[i] <= 'Z')
                val[sini[i]] = false;
            if(sini[i] != ' ')
                s += sini[i];
        }
    }
    n = s.size();
}

bool RezOR(int &ind);

bool Termen(int &ind)
{
    bool rez;
    if(s[ind] == '(')
    {
        ind++;
        rez = RezOR(ind);
        ind++;
    }
    else
    {
        bool adv = true;
        while(s[ind] == '!')
        {
            adv = !adv;
            ind++;
        }
        rez = val[s[ind]];
        if(!adv)
            rez = !rez;
    }
    return rez;
}

bool RezAND(int &ind)
{
    bool rez = Termen(ind);
    while(ind < n && s[ind] == '&')
    {
        bool crt = Termen(ind);
        rez &= crt;
        ind++;
    }
    return rez;
}

bool RezOR(int &ind)
{
    bool rez = RezAND(ind);
    while(ind < n && s[ind] == '|')
    {
        bool crt = RezAND(ind);
        rez |= crt;
        ind++;
    }
    return rez;
}

int main()
{
    getline(fin, sini);
    TransS();
    int k;
    fin >> k;
    val['0'] = 0;
    val['1'] = 1;
    for(int i = 0; i < k; i++)
    {
        char c;
        fin >> c;
        val[c] = !val[c];
        int ind = 0;
        fout << RezOR(ind);
    }
    return 0;
}