Pagini recente » Cod sursa (job #576306) | Cod sursa (job #3235560) | Cod sursa (job #525953) | Cod sursa (job #2879490) | Cod sursa (job #1798296)
#include<bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
string gb;
vector<vector<string>> ops;
map<char,int> h;
// is operation op, at location loc, when the string is st,dr
int is(string op, int loc,int st,int dr) {
if(loc + op.size() - 1 > dr) return 0;
int ok = 1;
for(int i=0;i<op.size() && ok;++i) {
if(gb[i+loc] != op[i]) ok = 0;
}
return ok;
}
// make the operations
int make(string op,int x,int y) {
if(op == "|") return x||y;
if(op == "&") return x&&y;
return 0;
}
// evals substring(st,dr), when we should only check lvl forward
int eval(int st,int dr, int lvl) {
for(int t = lvl;t<ops.size();++t) {
int par = 0;
for(int i = dr;i>=st;--i) {
if(gb[i] == '(') ++ par; if(gb[i] == ')') -- par;
if(par) continue;
for(auto op : ops[t]) {
if(is(op,i,st,dr)) {
return make(op,eval(st,i-1,t),eval(i+op.size(),dr,t));
}
}
}
}
if(gb[st] == '!') {
return !eval(st+1,dr,ops.size());
}
if(gb[st] == '(') {
return eval(st+1,dr-1,0);
}
if(gb[st] >= 'A' && gb[st] <= 'Z') {
return h[gb[st]]%2;
}
return gb[st] - '0';
}
//add operations in order, call at main start
void init() {
ops.pb(vector<string>({"|"}));
ops.pb(vector<string>({"&"}));
}
string s;
int make() {
gb = "";
int i = 0;
while(i < s.size()) {
if(s.substr(i,4) == "TRUE") {
gb.pb('1');
i+=4;
continue;
}
if(s.substr(i,3) == "FALSE") {
gb.pb('0');
i+=5;
continue;
}
if(s.substr(i,3) == "AND") {
gb.pb('&');
i+=3;
continue;
}
if(s.substr(i,3) == "NOT") {
gb.pb('!');
i+=3;
continue;
}
if(s.substr(i,2) == "OR") {
i+= 2;
gb.pb('|');
continue;
}
if(s[i] != ' ') {
gb.pb(s[i]);
}
++i;
}
}
int eval() {
return eval(0,gb.size()-1,0);
}
int main() {
freopen("bool.in","r",stdin);
freopen("bool.out","w",stdout);
init();
getline(cin,s);
make();
//cout << gb << endl;
int N;
cin >> N;
cin >> s;
for(auto c : s) {
h[c]++;
cout<<eval();
}
}