Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 16817 | Accepted: 6500 |
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
思路:把真值表枚举完就行。
下附代码:
1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 using namespace std; 5 const int MAXN = 120; 6 int sta[MAXN]; 7 char str[MAXN]; 8 int p, q, r, s, t; 9 void judge() { 10 int top = 0; 11 int len = strlen(str); 12 for (int i = len - 1; i >= 0; i--) { 13 if (str[i] == 'p') 14 sta[top++] = p; 15 else if (str[i] == 'q') 16 sta[top++] = q; 17 else if (str[i] == 'r') 18 sta[top++] = r; 19 else if (str[i] == 's') 20 sta[top++] = s; 21 else if (str[i] == 't') 22 sta[top++] = t; 23 else if (str[i] == 'K') { 24 int t1 = sta[--top]; 25 int t2 = sta[--top]; 26 sta[top++] = (t1 && t2); 27 } else if (str[i] == 'A') { 28 int t1 = sta[--top]; 29 int t2 = sta[--top]; 30 sta[top++] = (t1 || t2); 31 } else if (str[i] == 'N') { 32 int t1 = sta[--top]; 33 sta[top++] = (!t1); 34 } else if (str[i] == 'C') { 35 int t1 = sta[--top]; 36 int t2 = sta[--top]; 37 if (t1 == 1 && t2 == 0) 38 sta[top++] = 0; 39 else 40 sta[top++] = 1; 41 } else if (str[i] == 'E') { 42 int t1 = sta[--top]; 43 int t2 = sta[--top]; 44 if ((t1 == 1 && t2 == 1) || (t1 == 0 && t2 == 0)) 45 sta[top++] = 1; 46 else 47 sta[top++] = 0; 48 } 49 } 50 } 51 bool solve() { 52 for (p = 0; p < 2; p++) 53 for (q = 0; q < 2; q++) 54 for (r = 0; r < 2; r++) 55 for (s = 0; s < 2; s++) 56 for (t = 0; t < 2; t++) { 57 judge(); 58 if (sta[0] == 0) 59 return 0; 60 } 61 return 1; 62 } 63 int main() { 64 while (scanf("%s", str)) { 65 if (strcmp(str, "0") == 0) 66 break; 67 if (solve()) 68 printf("tautology "); 69 else 70 printf("not "); 71 } 72 return 0; 73 }