Cod sursa(job #2359575)

Utilizator aurelionutAurel Popa aurelionut Data 28 februarie 2019 22:11:17
Problema Bool Scor 50
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.92 kb
#include <iostream>

#include <fstream>
#include <cstring>

#define BRACKET -1
#define AND     -2
#define OR      -3
#define NOT     -4

using namespace std;

const int NMAX = 1005;
const int SIGMA = 26;
int n, m;
char s[NMAX], lit[NMAX];
bool val[SIGMA + 5];
int st[NMAX], top;

inline bool isLetter(char ch)
{
    return 'A' <= ch && ch <= 'Z';
}

inline bool CheckTrue(int i)
{
    return (s[i] == 'T' && s[i + 1] == 'R' && s[i + 2] == 'U' && s[i + 3] == 'E');
}

inline bool CheckFalse(int i)
{
    return (s[i] == 'F' && s[i + 1] == 'A' && s[i + 2] == 'L' && s[i + 3] == 'S' && s[i + 4] == 'E');
}

inline bool CheckNot(int i)
{
    return (s[i] == 'N' && s[i + 1] == 'O' && s[i + 2] == 'T');
}

inline bool CheckAnd(int i)
{
    return (s[i] == 'A' && s[i + 1] == 'N' && s[i + 2] == 'D');
}

inline bool CheckOr(int i)
{
    return (s[i] == 'O' && s[i + 1] == 'R');
}

bool Evaluate()
{
    top = 0;
    for (int i = 0;i <= n;++i)
    {
        if (s[i] == ' ')
            continue;
        if (s[i] == '(')
            st[++top] = BRACKET;
        else if (CheckTrue(i))
        {
            st[++top] = 1;
            i += 3;
        }
        else if (CheckFalse(i))
        {
            st[++top] = 0;
            i += 4;
        }
        else if (CheckNot(i))
        {
            st[++top] = NOT;
            i += 2;
        }
        else if (CheckAnd(i))
        {
            st[++top] = AND;
            i += 2;
        }
        else if (CheckOr(i))
        {
            i += 1;
        }
        else if (isLetter(s[i]))
        {
            bool x = val[s[i] - 'A'];
            while (top > 0 && st[top] == NOT)
            {
                x = !x;
                --top;
            }
            --top;
            if (top > 0 && st[top] == AND)
            {
                --top;
                x &= st[top];
                --top;
            }
            st[++top] = x;
        }
        else if (s[i] == ')')
        {
            int x = 0;
            while (top > 0 && st[top] != BRACKET)
            {
                x |= st[top];
                --top;
            }
            while (top > 0 && st[top] == NOT)
            {
                x = !x;
                --top;
            }
            if (top > 0 && st[top] == AND)
            {
                --top;
                x &= st[top];
                --top;
            }
            st[++top] = x;
        }
    }
    return st[1];
}

int main()
{
    ifstream fin("bool.in");
    ofstream fout("bool.out");
    fin.getline(s + 1, NMAX);
    n = strlen(s + 1);
    s[0] = '(';
    s[n + 1] = ')';
    n += 1;
    fin >> m;
    fin.get();
    fin.getline(lit + 1, NMAX);
    for (int i = 1;i <= m;++i)
    {
        val[lit[i] - 'A'] = !val[lit[i] - 'A'];
        fout << Evaluate();
    }
    fin.close();
    fout.close();
    return 0;
}