Pagini recente » Cod sursa (job #5828) | Cod sursa (job #2933537) | Istoria paginii runda/preoni2007_runda4_1112 | Cod sursa (job #134806) | Cod sursa (job #1586236)
#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; op='|';}
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();
}
else if(op){
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);
}
}
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;
}