Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 10437 | Accepted: 3963 |
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.
- 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.
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 |
A 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
p,q,r,s,t,是五个二进制数。
K,A,N,C,E,是五个运算符。
K:&&
A:||
N:!
C:(!w)||x
E:w==x
这道题可以将p,q,r,s,t穷举
1 #include <iostream> 2 #include<string.h> 3 #include <stack> 4 using namespace std; 5 int p, q, r, s, t; 6 int len; 7 char str[106]; 8 int result() { 9 stack<int> res; 10 int t1,t2; 11 for (int i = len - 1; i >= 0; i--) { 12 switch (str[i]) { 13 case 'p': 14 res.push(p); 15 break; 16 case 'q': 17 res.push(q); 18 break; 19 case 'r': 20 res.push(r); 21 break; 22 case 's': 23 res.push(s); 24 break; 25 case 't': 26 res.push(t); 27 break; 28 case 'K': 29 t1 = res.top(); 30 res.pop(); 31 t2 = res.top(); 32 res.pop(); 33 if (t1 & t2) { 34 res.push(1); 35 } else { 36 res.push(0); 37 } 38 break; 39 case 'A': 40 t1 = res.top(); 41 res.pop(); 42 t2 = res.top(); 43 res.pop(); 44 if (t1 | t2) 45 res.push(1); 46 else 47 res.push(0); 48 break; 49 case 'N': 50 t1 = res.top(); 51 res.pop(); 52 if (~t1 & 1) 53 res.push(1); 54 else 55 res.push(0); 56 break; 57 case 'C': 58 t1 = res.top(); 59 res.pop(); 60 t2 = res.top(); 61 res.pop(); 62 if ((~t1 & 1) | t2) { 63 res.push(1); 64 } else { 65 res.push(0); 66 } 67 break; 68 case 'E': 69 t1 = res.top(); 70 res.pop(); 71 t2 = res.top(); 72 res.pop(); 73 if (t1 == t2) { 74 res.push(1); 75 } else { 76 res.push(0); 77 } 78 break; 79 } 80 } 81 return res.top(); 82 } 83 84 int fun() { 85 int flag; 86 for (p = 0; p < 2; p++) { 87 for (q = 0; q < 2; q++) { 88 for (r = 0; r < 2; r++) { 89 for (s = 0; s < 2; s++) { 90 for (t = 0; t < 2; t++) { 91 flag = result(); 92 if(flag==0) 93 return 0; 94 } 95 } 96 } 97 } 98 } 99 return 1; 100 } 101 102 int main() { 103 while (cin >> str) { 104 if (strcmp(str, "0") == 0) 105 break; 106 len = strlen(str); 107 int flag=fun(); 108 if(flag) 109 cout<<"tautology"<<endl; 110 else 111 cout<<"not"<<endl; 112 } 113 }