POJ - 3295 Tautology(构造)

                                                  Tautology

Time Limit: 1000MS   Memory Limit: 65536K

Description

WFF 'N PROOF is a logic game played with dice. Each die has six faces representing some subset of the possible symbols K, A, N, C, E, p, q, r, s, t. A Well-formed formula (WFF) is any string of these symbols obeying the following rules:

  • p, q, r, s, and t are WFFs
  • if w is a WFF, Nw is a WFF
  • if w and x are WFFs, Kwx, Awx, Cwx, and Ewx are WFFs.

The meaning of a WFF is defined as follows:

  • p, q, r, s, and t are logical variables that may take on the value 0 (false) or 1 (true).
  • K, A, N, C, E mean and, or, not, implies, and equals as defined in the truth table below.

Definitions of K, A, N, C, and E

     w  x   Kwx   Awx    Nw   Cwx   Ewx
  1  1   1   1    0   1   1
  1  0   0   1    0   0   0
  0  1   0   1    1   1   0
  0  0   0   0    1   1   1

tautology is a WFF that has value 1 (true) regardless of the values of its variables. For example, ApNp is a tautology because it is true regardless of the value of p. On the other hand, ApNq is not, because it has the value 0 for p=0, q=1.

You must determine whether or not a WFF is a tautology.

Input

Input consists of several test cases. Each test case is a single line containing a WFF with no more than 100 symbols. A line containing 0 follows the last case.

Output

For each test case, output a line containing tautology or not as appropriate.

Sample Input

ApNp
ApNq
0

Sample Output

tautology
not

题意:K A N C E为运算符,数值p q r s t为true或false, 运算符的优先级相同。给你一个表达式,问是否恒为真。

解题思路:从后到前遍历表达式,遇到数值则将其入栈,遇到运算符则从栈中取出数值,经运算后再将新的数值存入栈。最后栈中剩下的唯一的数字即为表达式的值。因为p q r s t 的数值不确定,有32中可能组合,因此我用了32个栈来分别存,只要最后一个栈中最后剩下的布尔数的值为false,那么表达式就不恒为真。

ACCode:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <stack>

using namespace std;
bool get(int num,char ch)
{
	//p q r s t
	//32种数字组合
	switch (ch) {
		case 'p': return bool(num & 1);
		case 'q': return bool(num & 2);
		case 'r': return bool(num & 4);
		case 's': return bool(num & 8);
		case 't': return bool(num & 16);
	}
}
int main()
{
	char str[105];
	stack<bool > num[32];
	stack<char > op;
	while(~scanf("%s",str) && str[0] != '0') {
		for(int k=0;k<32;k++)
			while(!num[k].empty()) num[k].pop();
		while(!op.empty()) op.pop();
		
		int len = strlen(str)-1;
		bool t1,t2;
		for(int i=len;i>=0;i--) {
			if(str[i] > 'Z') {
				for(int k=0;k<32;k++)
					num[k].push(get(k,str[i]));
			}
			else {
				if(str[i] == 'K') {
					for(int k=0;k<32;k++) {
						t1 = num[k].top();
						num[k].pop();
						t2 = num[k].top();
						num[k].pop();
						num[k].push(t1 && t2);
					}
				}
				else if(str[i] == 'A'){
					for(int k=0;k<32;k++) {
						t1 = num[k].top();
						num[k].pop();
						t2 = num[k].top();
						num[k].pop();
						num[k].push(t1 || t2);
					}
				}
				else if(str[i] == 'N') {
					for(int k=0;k<32;k++) {
						t1 = num[k].top();
						num[k].pop();
						num[k].push(!t1);
					}
				}
				else if(str[i] == 'C') {
					for(int k=0;k<32;k++) {
						t1 = num[k].top();
						num[k].pop();
						t2 = num[k].top();
						num[k].pop();
						if(t1 && !t2) num[k].push(false);
						else num[k].push(true);
					}
				}
				else {
					for(int k=0;k<32;k++) {
						t1 = num[k].top();
						num[k].pop();
						t2 = num[k].top();
						num[k].pop();
						num[k].push(t1 == t2);
					}
				}
			}
		}
		int k;
		for(k=0;k<32;k++) {
			if(!num[k].top()) {
				printf("not\n");
				break;
			}
		}
		if(k == 32)
			printf("tautology\n");
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_42765557/article/details/97784068