Friday, May 15, 2015

UVa 11108 - Tautology

// UVa 11108 - Tautology

#include <iostream>
#include <stack>
#include <string>
#include <map>
using namespace std;

map<char, bool> val;
string s;
int k;

bool eval() {
	k++;
	if ('p' <= s[k] && s[k] <= 't')
		return val[s[k]];
	char sign = s[k];
	if (sign == 'K') {
		bool a = eval();
		bool b = eval();
		return a && b;
	} else if (sign == 'A') {
		bool a = eval();
		bool b = eval();
		return a || b;
	} else if (sign == 'N')
		return !eval();
	else if (sign == 'C') {
		bool a = eval();
		bool b = eval();
		return !a || b;
	} else
		//if (sign == 'E')
		return eval() == eval();
}

int main() {
	while ((cin >> s) && (s.length() > 0) && (s[0] != '0')) {
		bool tautology = true;
		for (int z = 0; z < 32; z++) {
			for (int c = 0; c <= 4; c++)
				val[c + 'p'] = (((z >> c) & 1) == 1);
			k = -1;
			if (!eval()) {
				tautology = false;
				break;
			}
		}
		if (tautology)
			cout << "tautology" << endl;
		else
			cout << "not" << endl;
	}
	return 0;
}

No comments:

Post a Comment