POJ3295——Tautology

Tautology

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, and t 为真假值。
      Kxy -> x&&y
      Axy -> x||y
      Nx -> !x
      Cxy -> x||(!y)
      Exy -> x==y
      判断是否表达式恒为真。
解题思路: 数值变量总共就5个,枚举这五个变量的值,有32种情况。
      处理字符串的时候,类似于逆波兰表达式的求值过程。
      从(S.length()-1)->0遍历,遇到小写字母将其对应的布尔值存入栈。
      遇到大写字母(除N外) 去除栈顶2个元素进行处理,后存入栈。
      遇到N,去除栈顶一个元素,取反后存入栈。
      遍历完返回S.top()。
Code:
 1 #include<iostream>
 2 #include<string>
 3 #include<stack>
 4 using namespace std;
 5 int q,p,s,r,t;
 6 bool Is_Tau(string S)
 7 {
 8     int len=S.length()-1,i,t1,t2;
 9     stack<char> ST;
10     for (i=len;i>=0;i--)
11     {
12         if (S[i]=='q') ST.push(q);
13         if (S[i]=='p') ST.push(p);
14         if (S[i]=='r') ST.push(r);
15         if (S[i]=='s') ST.push(s);
16         if (S[i]=='t') ST.push(t);
17         if (S[i]=='K')
18         {
19             t1=ST.top();
20             ST.pop();
21             t2=ST.top();
22             ST.pop();
23             ST.push(t1&&t2);
24         }
25         if (S[i]=='A')
26         {
27             t1=ST.top();
28             ST.pop();
29             t2=ST.top();
30             ST.pop();
31             ST.push(t1||t2);
32         }
33         if (S[i]=='C')
34         {
35             t1=ST.top();
36             ST.pop();
37             t2=ST.top();
38             ST.pop();
39             ST.push(t1||(!t2));
40         }
41         if (S[i]=='E')
42         {
43             t1=ST.top();
44             ST.pop();
45             t2=ST.top();
46             ST.pop();
47             ST.push(t1==t2);
48         }
49         if (S[i]=='N')
50         {
51             t1=ST.top();
52             ST.pop();
53             ST.push(!t1);
54         }
55     }
56     return ST.top();
57 }
58 int main()
59 {
60     string WFF;
61     while (cin>>WFF)
62     {
63         int OK=1;
64         if (WFF=="0") break;
65         for (q=0; q<=1; q++)
66             for (p=0; p<=1; p++)
67                 for (r=0; r<=1; r++)
68                     for (s=0; s<=1; s++)
69                         for (t=0; t<=1; t++)
70                             if (!Is_Tau(WFF))
71                             {
72                                 OK=0;
73                                 break;
74                             }
75         if (OK) printf("tautology
");
76         else printf("not
");
77     }
78     return 0;
79 }
原文地址:https://www.cnblogs.com/Enumz/p/3763572.html