Cod sursa(job #3143495)

Utilizator Alex_BerbescuBerbescu Alexandru Alex_Berbescu Data 30 iulie 2023 17:19:33
Problema Bool Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.26 kb
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("fast-math")
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
char s[1005], t[1005];
int k, u;
int fr[160], h;
int j;
int sau();
int si();
int neg();
int variabila();
int sau()
{
    int r = si();
    while(t[j] == '|')
    {
        j++;
        r = (r | si());
    }
    return r;
}
int si()
{
    int r = neg();
    while(t[j] == '&')
    {
        j++;
        r = (r & neg());
    }
    return r;
}
int neg()
{
    int r = variabila();
    while(t[j] == '^')
    {
        j++;
        r = (r ^ variabila());
    }
    return r;
}
int variabila()
{
    int r;
    if(t[j] == '(')
    {
        j++;
        r = sau();
        j++;
    }
    else
    {
        if(t[j] == '0')
        {
            j++;
            return 0;
        }
        if(t[j] == '1')
        {
            j++;
            return 1;
        }
        return fr[t[j++]];
    }
    return r;
}
ifstream fin("bool.in");
ofstream fout("bool.out");
int main()
{
    fin.getline(s, sizeof s);
    fin.get();
    int n = strlen(s);
    for(int i = 0; i < n; ++i)
    {
        if(s[i] == ' ')
        {
            continue;
        }
        if(s[i] == '(' || s[i] == ')')
        {
            t[++k] = s[i];
            continue;
        }
        if(s[i] == 'T' && s[i + 1] == 'R')
        {
            t[++k] = '1';
            i+=3;
            continue;
        }
        if(s[i] == 'A' && s[i + 1] == 'N')
        {
            t[++k] = '&';
            i+=2;
            continue;
        }
        if(s[i] == 'F' && s[i + 1] == 'A')
        {
            t[++k] = '0';
            i+=4;
            continue;
        }
        if(s[i] == 'O' && s[i + 1] == 'R')
        {
            t[++k] = '|';
            i++;
            continue;
        }
        if(s[i] == 'N' && s[i + 1] == 'O')
        {
            t[++k] = '1';
            t[++k] = '^';
            i+=2;
            continue;
        }
        t[++k] = s[i];
    }
    fin >> h;
    char carac;
    for(int i = 1; i <= h; ++i)
    {
        fin >> carac;
        fr[carac] = 1 - fr[carac];
        j = 0;
        fout << sau();
    }
    return 0;
}