http://poj.org/problem?id=3295
Meaning of the questions: lowercase letters to a number of bool variable capital letters bool operation, seeking the expression is true for the permanent expression.
Input of the form:
ApNp
ApNq
That is a prefix expression.
So I wrote something through it constructed a tree, and then give the same name as an enumeration variable assignment, if an assignment so that the roots do not have any output 0, was never really an expression.
Examine binary tree recursive traversal.
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<map>
#include<set>
#include<stack>
#include<string>
#include<queue>
#include<vector>
using namespace std;
typedef long long ll;
char s[1005];
int cur;
struct TreeNode {
char type;
vector<int> son;
} t[1005];
int top;
//建树
int build(int u) {
//cout<<"u="<<u<<endl;
t[u].son.clear();
if(islower(s[cur])) {
t[u].type = s[cur];
++cur;
return u;
} else if(isupper(s[cur])) {
t[u].type = s[cur];
++cur;
t[u].son.push_back(build(++top));
if(t[u].type != 'N')
t[u].son.push_back(build(++top));
return u;
}
}
bool calc(int u, int val) {
//cout<<"u="<<u<<endl;
//cout<<"t[u].type="<<t[u].type<<endl;
if(islower(t[u].type)) {
//cout<<((val >> (t[u].type - 'p')) & 1)<<endl;
return (val >> (t[u].type - 'p')) & 1;
} else {
bool val1 = calc(t[u].son[0], val);
if(t[u].type == 'N')
return !val1;
else {
bool val2 = calc(t[u].son[1], val);
if(t[u].type == 'K')
return val1 & val2;
if(t[u].type == 'A')
return val1 | val2;
if(t[u].type == 'E')
return val1 == val2;
if(t[u].type == 'C') {
if(val1 == 1 && val2 == 0)
return 0;
else
return 1;
}
exit(-1);
}
}
}
int main() {
#ifdef Yinku
freopen("Yinku.in", "r", stdin);
#endif // Yinku
while(~scanf("%s", s)) {
top = cur = 0;
if(s[0] == '0')
break;
build(++top);
bool all1 = 1;
for(int i = 0; i < (1 << 5); ++i) {
if(calc(1, i) == 0) {
all1 = 0;
break;
}
}
puts(all1 ? "tautology" : "not");
}
}