Cod sursa(job #2925854)

Utilizator Dragono63Stanciu Rares Stefan Dragono63 Data 16 octombrie 2022 11:43:34
Problema Bool Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.24 kb
#include <bits/stdc++.h>
#define pb push_back
#define pii pair<int, int>
using ll = long long;

using namespace std;

const int NMAX = 2005;
/*******************************/
// INPUT / OUTPUT

ifstream f("bool.in");
ofstream g("bool.out");
/*******************************/
/// GLOBAL DECLARATIONS

char s[NMAX];
int N, M;
int numIndex, opIndex;
bool q[NMAX];
string op[NMAX];
bool val[26];
/*******************************/
/// FUNCTIONS

void ReadInput();
void Solution();
void Output();
/*******************************/
///-------------------------------------
inline void ReadInput()
{
    f.getline(s, NMAX);
    N = strlen(s);
    f >> M;
}
///-------------------------------------
int grad(string operatie)
{
    if (operatie == "OR") return 1;
    if (operatie == "AND") return 2;
    if (operatie == "NOT") return 3;
    return 0;
}
///-------------------------------------
void calculeaza(const int &a, const int &b)
{
    if (op[b] == "NOT")
    {
        q[a] = !q[a];
    }

    if (op[b] == "AND")
    {
        q[a - 1] &= q[a];
    }

    if (op[b] == "OR")
    {
        q[a - 1] |= q[a];
    }
}
///-------------------------------------
void eval()
{
    opIndex = 0, numIndex = 0;
    string tmp;
    char ch;
    for (int i = 0 ; i < N ; ++ i)
    {
        tmp = "";
        if ('A' <= s[i] && s[i] <= 'Z')
        {   
            while ('A' <= s[i] && s[i] <= 'Z')
            {
                tmp += s[i];
                i ++;
            }

            if (tmp.length() == 1)
            {
                ch = tmp[0];
                q[++numIndex] = val[ch - 'A'];
                i --;
                continue;
            }

            if (tmp == "TRUE")
            {
                q[++ numIndex] = true;
                i --;
                continue;
            }

            if (tmp == "FALSE")
            {
                q[++ numIndex] = true;
                i --;
                continue;
            }
        }
        else
        {
            if (s[i] == ' ') continue;
            tmp += s[i];
        }

        if (opIndex == 0 || tmp == "(" || grad(tmp) > grad(op[opIndex]))
        {
            op[++opIndex] = tmp;
        }
        else
        {
            if (tmp == ")")
            {
                while (op[opIndex] == "(")
                {
                    calculeaza(numIndex, opIndex);
                    numIndex --, opIndex --;
                }
                opIndex --;
            }
            else
            {
                while (grad(tmp) < grad(op[opIndex]))
                {
                    calculeaza(numIndex, opIndex);
                    numIndex --, opIndex --;
                }
                op[++opIndex] = tmp;
            }
        }
    }

    while (opIndex)
    {
        calculeaza(numIndex, opIndex);
        numIndex --, opIndex --;
    }
}
///-------------------------------------
inline void Solution()
{
    char ch;
    while (M --)
    {
        f >> ch;
        val[ch - 'A'] = !val[ch - 'A']; 
        eval();
        g << q[1];
    }
}
///-------------------------------------
inline void Output()
{

}
///-------------------------------------
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ReadInput();
    Solution();
    Output();
    return 0;
}