poj 3295Tautology

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

做这题的时候正好学了离散- -!。。一开始想的是从前面开始模拟,发现不容易实现,因为比如要实现Apq,告诉了p,但是后面部分还是个算式的话就不容易
算了。所以想到了从后往前不断递推。。这样的话最后栈内的结果就是最后的答案
View Code
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <stack>
 4 using namespace std;
 5 int p,q,r,s,t;
 6 int charge(char c)
 7 {
 8     if(c=='p') return p;
 9     if(c=='q') return q;
10     if(c=='r') return r;
11     if(c=='s') return s;
12     if(c=='t') return t;
13 }
14 int main()
15 {
16     char str[110];
17     int i,x,y;
18     while(scanf("%s",str)&&str[0]!='0')
19     {
20         int flag=1;
21         stack<int>st;
22         for(p=0;p<=1&&flag;p++)
23             for(q=0;q<=1&&flag;q++)
24                 for(r=0;r<=1&&flag;r++)
25                     for(s=0;s<=1&&flag;s++)
26                         for(t=0;t<=1&&flag;t++)
27                         {
28                             for(i=strlen(str)-1;i>=0;i--)
29                             {
30                                 if(str[i]=='p'||str[i]=='q'||str[i]=='r'
31                                    ||str[i]=='s'||str[i]=='t')
32                                    st.push(charge(str[i]));
33                                 else if(str[i]=='K')
34                                 {
35                                     x=st.top();
36                                     st.pop();
37                                     y=st.top();
38                                     st.pop();
39                                     st.push(x&&y);
40                                 }
41                                 else if(str[i]=='A')
42                                 {
43                                     x=st.top();
44                                     st.pop();
45                                     y=st.top();
46                                     st.pop();
47                                     st.push(x||y);
48                                 }
49                                 else if(str[i]=='N')
50                                 {
51                                     x=st.top();
52                                     st.pop();
53                                     st.push(!x);
54                                 }
55                                 else if(str[i]=='C')
56                                 {
57                                     x=st.top();
58                                     st.pop();
59                                     y=st.top();
60                                     st.pop();
61                                     if(x==1&&y==0) st.push(0);
62                                     else st.push(1);
63                                 }
64                                 else if(str[i]=='E')
65                                 {
66                                     x=st.top();
67                                     st.pop();
68                                     y=st.top();
69                                     st.pop();
70                                     st.push((x==y));
71                                 }
72                                 else
73                                     flag=0;
74                             }
75                             if(flag)
76                             {
77                                 flag=st.top();
78                                 st.pop();
79                             }
80                         }
81         if(flag)
82             printf("tautology\n");
83         else
84             printf("not\n");
85     }
86     return 0;
87 }
原文地址:https://www.cnblogs.com/wilsonjuxta/p/2963702.html