Cod sursa(job #1525404)

Utilizator mouse_wirelessMouse Wireless mouse_wireless Data 15 noiembrie 2015 00:09:16
Problema Bool Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.35 kb
#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <vector>
#include <cstring>
#include <cctype>
using namespace std;

#ifdef INFOARENA
#define ProblemName "bool"
#endif

#define MCONCAT(A, B) A B
#ifdef ProblemName
#define InFile MCONCAT(ProblemName, ".in")
#define OuFile MCONCAT(ProblemName, ".out")
#else
#define InFile "fis.in"
#define OuFile "fis.out"
#endif

template <class T> void readNum(T &nr) {
	nr = 0;
	T sign = 1;
	char c;
	while (!isdigit(c = getchar()))
		(c == '-') && (sign = -1);
	do {
		nr = nr * 10 + c - '0';
	} while (isdigit(c = getchar()));
	nr *= sign;
}

#include <stack>

class _prec {
private:
	vector<char> v;
public:
	_prec() {
		v.resize(255);
		memset(&v[0], 0, sizeof(v[0]) * v.size());
		v['('] = 1;
		v['o'] = 2;
		v['a'] = 3;
		v['n'] = 4;
	}
	char operator()(char c) {
		return v[c];
	}
} prec;

#define AND_HASH 211
#define OR_HASH 161
#define NOT_HASH 241
#define FALSE_HASH 363
#define TRUE_HASH 320
class _decoder {
private:
	vector<char> v;
public:
	_decoder() {
		v.resize(512, 0);
		for (char i = 'A'; i <= 'Z'; i++)
			v[i] = i;
		v[AND_HASH] = 'a';
		v[OR_HASH] = 'o';
		v[NOT_HASH] = 'n';
		v[FALSE_HASH] = 'f';
		v[TRUE_HASH] = 't';
	}
	char operator()(int c) {
		return v[c];
	}
} decoder;

class bintree {
private:
	char label;
	bintree *left, *right;
public:
	bintree(int V, bintree *L, bintree *R) : label(V), left(L), right(R) {};
	char top() { return label; }
	char eval(vector<char>& truthval) {
		if (!left && !right)
			return truthval[label];
		if (label == 'n')
			return 1 - left->eval(truthval);
		if (label == 'a')
			return (right->eval(truthval) & right->eval(truthval));
		if (label == 'o')
			return (right->eval(truthval) | right->eval(truthval));
		return 0;
	}
};

void mktree(stack<char>& S1, stack<bintree*>& S2) {
	char op = S1.top();
	S1.pop();
	bintree* n1 = S2.top();
	S2.pop();
	if (op == 'n') {
		S2.push(new bintree(op, n1, NULL));
		return;
	}
	bintree* n2 = S2.top();
	S2.pop();
	S2.push(new bintree(op, n1, n2));
}

bintree* parseinput() {
	char c;
	stack<char> S1; stack<bintree*> S2;
	int curOp = 0; bool inOp = false;
	while ((c = getchar()) && c != '\n') {
		if (c <= 'Z' && c >= 'A') {
			curOp += c;
			inOp = true;
			continue;
		}
		if (inOp) {
			char aux = decoder(curOp);
			if (aux == curOp || aux == 't' || aux == 'f')
				S2.push(new bintree(aux, NULL, NULL));
			else if (prec(aux)) {
				while (!S1.empty() && prec(aux) <= prec(S1.top()))
					mktree(S1, S2);
				S1.push(aux);
			}
			curOp = 0;
			inOp = false;
		}
		if (c == '(') {
			S1.push(c);
			continue;
		}
		if (c == ')') {
			while (S1.top() != '(')
				mktree(S1, S2);
			S1.pop();
			continue;
		}		
	}
	if (inOp) {
		char aux = decoder(curOp);
		if (aux == curOp || aux == 't' || aux == 'f')
			S2.push(new bintree(aux, NULL, NULL));
	}
	while (!S1.empty())
		mktree(S1, S2);
	bintree* retval = S2.top();
	S2.pop();
	return retval;
}

int main() {
	assert(freopen(InFile, "r", stdin));
	assert(freopen(OuFile, "w", stdout));
	bintree* bt = parseinput();
	vector<char> truthval(255, 0);
	truthval['t'] = 1;
	int N; readNum(N);
	while (N--) {
		char c = getchar();
		truthval[c] = 1 - truthval[c];
		if (bt->eval(truthval))
			putchar('1');
		else
			putchar('0');
	}
	return 0;
}