Tautology
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 6949 | Accepted: 2653 |
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
Source


1 #include <iostream> 2 #include <cstdio> 3 #include <stack> 4 #include <cstring> 5 using namespace std; 6 7 stack <int> s; 8 9 void fk() 10 { 11 int a = s.top(); 12 s.pop(); 13 int b = s.top(); 14 s.pop(); 15 a &= b; 16 s.push(a); 17 } 18 19 void fa() 20 { 21 int a = s.top(); 22 s.pop(); 23 int b = s.top(); 24 s.pop(); 25 a |= b; 26 s.push(a); 27 } 28 29 void fc() 30 { 31 int a = s.top(); 32 s.pop(); 33 int b = s.top(); 34 s.pop(); 35 a = a<=b ? 1 : 0; 36 s.push(a); 37 } 38 39 void fe() 40 { 41 int a = s.top(); 42 s.pop(); 43 int b = s.top(); 44 s.pop(); 45 a = (a==b) ? 1 : 0; 46 s.push(a); 47 } 48 49 void fn() 50 { 51 int a = s.top(); 52 s.pop(); 53 s.push(!a); 54 } 55 56 int main() 57 { 58 // freopen("in.txt","r",stdin); 59 int k; 60 int i; 61 int a[5]; 62 char arr[1005]; 63 int flag; 64 while (scanf("%s",arr)) 65 { 66 flag = 1; 67 if (arr[0] == '0') break; 68 for (k=0; k<32; k++) 69 { 70 int temp = k; 71 for (i=4; i>=0; i--) 72 { 73 a[i] = temp%2; 74 temp = temp>>1; 75 } 76 int len = strlen(arr); 77 for (i=len-1; i>=0; i--) 78 { 79 if (arr[i] == 'K') fk(); 80 else if (arr[i] == 'A') fa(); 81 else if (arr[i] == 'N') fn(); 82 else if (arr[i] == 'C') fc(); 83 else if (arr[i] == 'E') fe(); 84 else 85 { 86 s.push(a[arr[i]-'p']); 87 } 88 } 89 int tmp = s.top(); 90 if (tmp != 1) 91 { 92 flag = 0; 93 break; 94 } 95 } 96 if (flag) 97 printf("tautology\n"); 98 else 99 printf("not\n"); 100 } 101 return 0; 102 }