由题意知,有5种操作,5个未知数,可0可1,一串操作问是否恒为1,最多100个字符,直接栈模拟所有情况即可
代码如下:
int p, q, r, s, t;
bool calculate(string ind) {
int length = ind.size();
stack<int> buf;
for (int i = length - 1; i >= 0;--i) {
char tmp = ind[i];
if(tmp == 'p')
buf.push(p);
else if(tmp == 'q')
buf.push(q);
else if(tmp == 'r')
buf.push(r);
else if(tmp == 's')
buf.push(s);
else if(tmp == 't')
buf.push(t);
else if(tmp == 'K') {
int val1, val2;
val1 = buf.top(), buf.pop();
val2 = buf.top(), buf.pop();
buf.push(val1 & val2);
}
else if(tmp == 'A') {
int val1, val2;
val1 = buf.top(), buf.pop();
val2 = buf.top(), buf.pop();
buf.push(val1 || val2);
}
else if(tmp == 'N') {
int val = buf.top();
buf.pop();
buf.push(!val);
}
else if(tmp == 'C') {
int val1, val2;
val1 = buf.top(), buf.pop();
val2 = buf.top(), buf.pop();
buf.push(!val1 || val2);
}
else if(tmp == 'E') {
int val1, val2;
val1 = buf.top(), buf.pop();
val2 = buf.top(), buf.pop();
buf.push(val1 == val2);
}
}
return buf.top();
}
int main() {
string str;
while(cin >> str && str != "0") {
bool flag = true;
for (p = 0; p < 2;++p)
{
for (q = 0; q < 2;++q) {
for (r = 0; r < 2;++r) {
for (s = 0; s < 2;++s) {
for (t = 0; t < 2;++t) {
if(!calculate(str)) {
flag = false;
break;
}
}
if(!flag)break;
}
if(!flag)break;
}
if(!flag)break;
}
if(!flag)break;
}
if(flag)
printf("tautology
");
else
printf("not
");
}
return 0;
}