Cod sursa(job #985219)

Utilizator andrettiAndretti Naiden andretti Data 16 august 2013 22:28:42
Problema Bool Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.47 kb
#include<stdio.h>
#include<cstring>
#define maxn 105
#define maxl 1005
#define maxalf 27
using namespace std;

int n,m,k;
int val[maxalf];
char s[maxl];

int disjunct();
int conjunct();
int exp();

void read()
{
    fgets(s+1,sizeof(s),stdin);
    n=strlen(s+1)-1;
}

void spaces(){
    while(s[k]==' ') k++;
}

int non(int val){
    if(val) return 0;
    return 1;
}

int exp()
{
    spaces();
    int sol;
    if(s[k]=='N' && s[k+1]=='O') {k+=3; spaces(); return !exp();}

    if(s[k]=='T' && s[k+1]=='R') k+=4,sol=1;
     else
      if(s[k]=='F' && s[k+1]=='A') k+=5,sol=0;
      else
        if(s[k]>='A' && s[k]<='Z') k++,sol=val[s[k-1]-'A'];
        else
         if(s[k]=='(') k++,sol=disjunct(),k++;

    spaces();
    return sol;
}

int conjunct()
{
    spaces();
    int sol=exp();
    while(s[k]=='A' && s[k+1]=='N' && s[k+2]=='D')
     k+=3,sol&=exp();
    spaces();
    return sol;
}

int disjunct()
{
    spaces();
    int sol=conjunct();
    while(s[k]=='O' && s[k+1]=='R')
     k+=2,sol|=conjunct();
    spaces();
    return sol;
}

void solve()
{
    char ch;
    scanf("%d\n",&m);
    for(int i=1;i<=m;i++)
    {
        scanf("%c",&ch);
        val[ch-'A']=non(val[ch-'A']);
        k=1;
        printf("%d",disjunct());
    }
}

int main()
{
    freopen("bool.in","r",stdin);
    freopen("bool.out","w",stdout);

    read();
    solve();

    fclose(stdin);
    fclose(stdout);
    return 0;
}