Cod sursa(job #3250868)

Utilizator andrei1232008nicolae andrei andrei1232008 Data 23 octombrie 2024 21:56:50
Problema Bool Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.79 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin("bool.in");
ofstream fout("bool.out");
bool nu();
bool si();
bool sau();
bool fact();
void debut();
int i,n,par;
string s,c,constr,l;
char car;
unordered_map <int,bool> v;
int main()
{
    while(fin>>constr&&isdigit(constr[0])==0)
    {
        s+=constr;
    }
    debut();
    n=stoi(constr);
    for(i=1;i<=n;i++)
    {fin>>l[i];v[(int)l[i]]=0;}
    for(par=1;par<=n;par++)
    {
        i=0;
        v[(int)l[par]]=1-v[(int)l[par]];
        fout<<sau();
    }
    return 0;
}
void debut()
{
    for(i=0;i<s.size();i++)
    {
        if(i+2<s.size()&&s[i]=='A'&&s[i+1]=='N'&&s[i+2]=='D')
            c+='&',i+=2;
        else if(i+2<s.size()&&s[i]=='N'&&s[i+1]=='O'&&s[i+2]=='T')
        c+='!',i+=2;
        else if(i+1<s.size()&&s[i]=='O'&&s[i+1]=='R')
        c+='|',i++;
        else if(i+3<s.size()&&s[i]=='T'&&s[i+1]=='R'&&s[i+2]=='U'&&s[i+3]=='E')
        c+='1',i+=3;
        else if(i+4<s.size()&&s[i]=='F'&&s[i+1]=='A'&&s[i+2]=='L'&&s[i+3]=='S'&&s[i+4]=='E')
        c+='0',i+=4;
        else c+=s[i];
    }
}
bool sau()
{
    bool x=si();
    while(i<c.size()&&c[i]=='|')
    {
        i++;
        x|=si();
    }
    return x;
}
bool si()
{
    bool x=nu();
    while(i<c.size()&&c[i]=='&')
    {
        i++;
        x&=nu();
    }
    return x;
}
bool nu()
{
    if(c[i]=='!')
    {
        while(i<c.size()&&c[i]=='!')
        {
            i++;
            return 1-nu();
        }
    }
    else return fact();
}
bool fact()
{
    bool x=0;
    if(c[i]=='(')
    {
        i++;
        x=sau();
        i++;
    }
    else
    {
        if(isdigit(c[i]))x=c[i]-'0';
        else if(c[i]>='A'&&c[i]<='Z')x=v[(int)c[i]];
        i++;
    }
    return x;
}