Cod sursa(job #1887977)

Utilizator andru47Stefanescu Andru andru47 Data 21 februarie 2017 21:02:11
Problema Bool Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.47 kb
#include <bits/stdc++.h>
using namespace std;
char s[1005];
string str;
int val[30], ind;
inline int orr();
inline int nott()
{
    int nr = 0;
    if (str[ind] == '(')
        ++ind, nr = orr(), ++ind;
    else if (str[ind] == '!')
    {
        ++ind;
        nr =  nott() ^ 1;
    }
    else
         nr = val[str[ind] - 'A'], ++ind;
    return nr;
}
inline int andd()
{
    int nr = nott();
    while(str[ind] == '&')
       ++ind ,  nr &= nott();
    return nr;
}
inline int orr()
{
    int nr = andd();
    while (str[ind] == '|')
        ++ind , nr |= andd();
    return nr;
}
int main()
{
    freopen("bool.in","r",stdin);
    freopen("bool.out","w",stdout);

    gets(s + 1);
    int len = strlen (s + 1);
    for (int i = 1; i<=len; ++i)
    {
        if (s[i] == 'A' && s[i + 1] == 'N')
            str += '&', i+=2;
        else if (s[i] == 'O' && s[i + 1] == 'R')
            str += '|', ++i;
        else if (s[i] == 'N' && s[i + 1] == 'O')
            str += '!', i+=2;
        else if (s[i] == 'T' && s[i + 1] == 'R')
            str += '1', i += 3;
        else if (s[i] == 'F' && s[i + 1] == 'A')
            str += '0', i += 4;
        else if (isalpha(s[i]) || s[i] == '(')
            str += s[i] , ++i;

    }
    int Q;
    str += ' ';
    for (scanf("%d\n", &Q); Q; --Q)
    {
        char c;
        scanf("%c", &c);
        val[c - 'A'] ^= 1;
        ind = 0;
        printf("%d",orr());
    }

    return 0;
}