Cod sursa(job #3337781)

Utilizator nicoleta_iancuIancu Nicoleta nicoleta_iancu Data 30 ianuarie 2026 00:01:06
Problema Bool Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 4.41 kb
#include<iostream>
#include<fstream>
#include<string>
#include<algorithm>
#include<vector>
#include<unordered_map>
using namespace std;

ifstream fin("bool.in");
ofstream fout("bool.out");
int maxLetters = 'z' - 'a' + 1;
vector<bool>letters(maxLetters);
int n;
unordered_map<string, char>val = { {"AND",'&'} ,{"NOT",'~'},{"OR",'|'},{"TRUE",'1'},{"FALSE",'0'} };
bool OR(string& s, int& pozi);
bool AND(string& s, int& pozi);
bool NOT(string& s, int& pozi);
bool expresie(string& s, int& pozi);
bool lit(string& s, int& pozi);

bool OR(string& s, int& pozi) {

    //  fout<<pozi<<endl;

    bool aux;
    bool rez = AND(s, pozi);
    while (pozi < n && s[pozi] == '|') {

        ++pozi;
        aux = AND(s, pozi);
        // cout<<"OR rez:"<<rez<<" aux:"<<aux<<endl;
      //  cout <<s[pozi]<<" "<< aux << " " << rez << endl;
        rez = aux | rez;
    }
    return rez;
}

bool AND(string& s, int& pozi) {

    bool rez = NOT(s, pozi);
    bool aux = 0;
    while (pozi < n && s[pozi] == '&') {

        ++pozi;

        aux = NOT(s, pozi);

        //  cout<<rez<<" "<<aux<<endl;
        //  cout<<"AND rez:"<<rez<<" aux:"<<aux<<" ";
        rez = aux & rez;
        //   cout<<rez<<endl;
    }
    return rez;
}

bool NOT(string& s, int& pozi) {

    bool semn = true;
    // cout<<s[pozi]<<endl;
    while (pozi < n && s[pozi] == '~') {

        semn = !semn;
        ++pozi;
    }
    //  cout<<"NOT:"<<s[pozi]<<" "<<semn<<endl;
    bool rez = expresie(s, pozi);
    //  cout<<rez<<" "<<semn<<endl;
    if (semn) {
        return rez;
    }

    return !rez;
}

bool expresie(string& s, int& pozi) {

    bool rez = 0;

    if (pozi < n && s[pozi] == '(') {
        ++pozi;
        rez = OR(s, pozi);
        ++pozi;
        return rez;
    }
    else {
        //cout<<s[pozi]<<" "<<lit(s,pozi)<<" "<<endl;
       // --pozi;
        return lit(s, pozi);
    }
}
bool lit(string& s, int& pozi) {

    //cout<<pozi<<" "<<s[pozi]<<endl;
    if (isdigit(s[pozi])) {
        ++pozi;
        return s[pozi - 1] - '0';
    }
    else {
        ++pozi;
        return letters[s[pozi - 1] - 'A'];
    }
    return 0;
}
string trueString = "TRUE";
string falseString = "FALSE";
string transformString(string& s) {
    string cuv;
    string rez;
    for (int i = 0; i < s.size(); ++i) {
        if (s[i] == ' ') {
            if (val.count(cuv)) {
                rez.push_back(val[cuv]);
                cuv.clear();
            }
            else {
                rez.insert(rez.end(), cuv.begin(), cuv.end());
                cuv.clear();
            }
        }
        else {
            cuv.push_back(s[i]);
        }
        int k = cuv.size() - 1;
        if (cuv.size() >= 5 && cuv[k] == 'E' && cuv[k - 1] == 'S' && cuv[k - 2] == 'L' && cuv[k - 3] == 'A' && cuv[k - 4] == 'F') {
            cuv.erase(cuv.end() - 5, cuv.end());
            cuv.push_back('0');
        }
        else if (cuv.size() >= 4 && cuv[k] == 'E' && cuv[k - 1] == 'U' && cuv[k - 2] == 'R' && cuv[k - 3] == 'T') {
            //  fout<<"TEST"<<endl;
            cuv.erase(cuv.end() - 4, cuv.end());
            cuv.push_back('1');
        }
        else if (cuv.size() >= 3 && cuv[k] == 'T' && cuv[k - 1] == 'O' && cuv[k - 2] == 'N') {
            cuv.erase(cuv.end() - 3, cuv.end());
            cuv.push_back('~');
        }
        else if (cuv.size() >= 3 && cuv[k] == 'D' && cuv[k - 1] == 'N' && cuv[k - 2] == 'A') {
            cuv.erase(cuv.end() - 3, cuv.end());
            cuv.push_back('&');
        }
        else if (cuv.size() >= 2 && cuv[k] == 'R' && cuv[k - 1] == 'O') {
            cuv.erase(cuv.end() - 2, cuv.end());
            cuv.push_back('|');
        }

    }
    if (val.count(cuv)) {
        rez.push_back(val[cuv]);
    }
    else {
        rez.insert(rez.end(), cuv.begin(), cuv.end());
    }
    return rez;
}
int main() {
    string s;
    getline(fin, s);
    s = transformString(s);
    // fout<<s<<endl;
    n = s.size();
   //  fout<<endl<<s;
    int questions;
    fin >> questions;
    char c;
    int pozStart = 0;
    // cout<<s<<endl;
    for (int i = 0; i < questions; ++i) {
        fin >> c;
        // cout<<i<<endl;
        letters[c - 'A'] = !letters[c - 'A'];
        pozStart = 0;
        //fout<<"lit:"<<letters[0]<<" "<<endl;
        fout << OR(s, pozStart);
        //cout<<"TEST"<<endl;
    }
    return 0;
}