Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 8533 | Accepted: 3275 |
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:
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 |
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
# include<stdio.h> # include<string.h> # include<stdlib.h> char wff[210]; int stack[210]; int caller(char c,int w,int x) { switch(c) { case'K': return w & x; case'A': return w | x; case'C': return !w | x; case'E': return w == x; } } int getv(char c,int p,int q,int r,int s,int t) { switch (c) { case 'p': return p; case 'q': return q; case 'r': return r; case 's': return s; case 't': return t; } } int main() { // freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); int i,len,top,state; int p,q,r,s,t; memset(wff,0,sizeof(wff)); memset(stack,0,sizeof(stack)); while(scanf("%s",wff) && wff[0]!='0') { len = strlen(wff); state = 1; for(p=0;p<=1;p++) { for(q=0;q<=1;q++) { for(r=0;r<=1;r++) { for(s=0;s<=1;s++) { for(t=0;t<=1;t++) { top = 1; memset(stack,0,sizeof(stack)); for(i=len-1;i>=0;i--) { if(wff[i]>='p' && wff[i]<='t') stack[top++] = getv(wff[i],p,q,r,s,t); else if(wff[i]=='K'|| wff[i]=='A'|| wff[i]=='C'|| wff[i]=='E') { stack[top-2] = caller(wff[i],stack[top-1],stack[top-2]); top--; } else if(wff[i]=='N') { stack[top-1] = (1 - stack[top-1]); } } if(!stack[top-1]) { state = 0; break; } } } } } } if(state==1) printf("tautology\n"); else printf("not\n"); memset(wff,0,sizeof(wff)); } return 0; }