Cod sursa(job #3337685)

Utilizator nicoleta_iancuIancu Nicoleta nicoleta_iancu Data 29 ianuarie 2026 15:26:16
Problema Bool Scor 30
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.23 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;
        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<<"AND rez:"<<rez<<" aux:"<<aux<<endl;
        rez=aux&rez;
    }
    return rez;
}

bool NOT(string &s,int &pozi){
    bool semn=true;
    while(s[pozi]=='~'){ 
        semn=!semn;
        ++pozi;
    }
   // cout<<"NOT:"<<s[pozi]<<" "<<semn<<endl;
   bool rez=expresie(s,pozi);
    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');
        }
        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');
        }
    }
    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;
    for(int i=0;i<questions;++i){
        fin>>c;
       // cout<<i<<endl;
        letters[c-'A']=!letters[c-'A'];
        pozStart=0;
        //fout<<letters[0]<<" ";
        fout<<OR(s,pozStart);
        //cout<<"TEST"<<endl;
    }
    return 0;
}