Cod sursa(job #1586311)

Utilizator oldatlantianSerban Cercelescu oldatlantian Data 31 ianuarie 2016 23:55:21
Problema Bool Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.58 kb
#include <algorithm>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <map>
#include <stack>
#include <vector>
using namespace std;

char str[1005],line[1005];
map<char, bool> mp;
int pr[256];

bool eval(void){
    stack<bool> nums;
    stack<char> ops;
    int t;
    char op;

    for(int i=0; str[i]; ++i){
        if(isalpha(str[i]) && !isalpha(str[i+1]))
            {nums.push(mp[str[i]]); continue;}
        if(str[i]=='(')
            {ops.push('('); continue;}

        op=false;
        if(str[i]=='N' && str[i+1]=='O' && str[i+2]=='T'){i+=2; op='~';}
        else if(str[i]=='A' && str[i+1]=='N' && str[i+2]=='D'){i+=2; op='&';}
        else if(str[i]=='O' && str[i+1]=='R'){i+=1;  op='|'; }
        else if(str[i]=='F' && str[i+1]=='A' && str[i+2]=='L' && str[i+3]=='S' && str[i+4]=='E'){i+=4; nums.push(0); continue;}
        else if(str[i]=='T' && str[i+1]=='R' && str[i+2]=='U' && str[i+3]=='E'){i+=3; nums.push(1); continue;}

        if(op){
            if(op=='~' && ops.top()=='~')
                {ops.pop(); continue;}
            while(ops.top()!='(' && pr[ops.top()]>=pr[op]){
                t=nums.top();
                nums.pop();
                switch(ops.top()){
                case '&':
                    nums.top()&=t;
                    break;
                case '|':
                    nums.top()|=t;
                    break;
                case '~':
                    nums.push(t^1);
                    break;
                }
                ops.pop();
            }
            ops.push(op);
        }
        else if(str[i]==')'){
            while(ops.top()!='(' && pr[ops.top()]>=pr[op]){
                t=nums.top();
                nums.pop();
                switch(ops.top()){
                case '&':
                    nums.top()&=t;
                    break;
                case '|':
                    nums.top()|=t;
                    break;
                case '~':
                    nums.push(t^1);
                    break;
                }
                ops.pop();
            }
            ops.pop();
        }

    }
    return nums.top();
}

int main(void){
    freopen("bool.in","r",stdin);
    freopen("bool.out","w",stdout);
    pr['('] = 0;
    pr['|'] = 1;
    pr['&'] = 2;
    pr['~'] = 3;
    pr[')'] = 4;

    int n;
    gets(str+1);
    gets(line); sscanf(line,"%d",&n);
    gets(line);

    str[0]='(';
    str[strlen(str)]=')';

    for(int i=0; line[i]; ++i){
        mp[line[i]]^=1;
        printf("%d",eval());
    }
    return 0;
}