Tautology
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 9936 |
|
Accepted: 3774 |
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, N-W is a WFF
- If w and x are wffs, Kwx, Awx, Cwx, and Ewx is wffs.
The meaning of a WFF is defined as follows:
- P, Q, R, S, and T is logical variables that could 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 |
K wx |
A wx |
N w |
C wx |
E wx |
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 have value 1 (true) regardless of the values of its variables. For example, Apnp are a tautology because it is true regardless of the value of p. On the other hand, Apnq was not, because it had 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 was a single line containing a WFF with no more than symbols. A line containing 0 follows.
Output
For each test case, the output a line containing tautology or not as appropriate.
Sample Input
ApNpApNq0
Sample Output
Tautologynot
Test instructions: Enter a logical expression consisting of 10 letters of P, Q, R, S, T, K, A, N, C, E,
where P, Q, R, S, T value is 1 (true) or 0 (false), that is, the logical variable;
K, A, N, C, E are logical operators,
K--and:x && y
A--Or:x | | Y
N--not:!x
C--implies: (!x) | | Y
E--Equals:x==y
Ask if this logical expression is a perpetual truth.
Idea: The WFF method: reads the character from the end of the string wff, sequentially. Constructs a stack stack, when encounters the logical variable p, Q, R, S, T then will its current value presses the stack, encounters N then takes the stack top element to carry on the non-operation, the result of the operation value presses the stack; encountered K, A, C, E is popped from the top of the stack two elements to the corresponding operation, the value of Because the input is legal, when the string WFF scan ends, only one value is left in the stack stack, which is the value of the logical expression WFF.
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <iostream> #include < Algorithm> #include <stack>using namespace Std;char str[110];stack<int>q;int p,q,r,s,t,len;int IN_ Stack () {int i; int x, y; for (I=strlen (str)-1; i>=0; i--) {if (str[i] = = ' P ') Q.push (p); else if (str[i] = = ' Q ') Q.push (q); else if (str[i] = = ' R ') Q.push (R); else if (str[i] = = ' s ') Q.push (s); else if (str[i] = = ' t ') q.push (t); else if (str[i]== ' K ') {x = Q.top (); Q.pop (); y = Q.top (); Q.pop (); Q.push (x && y); } else if (str[i] = = ' A ') {x = Q.top (); Q.pop (); y = Q.top (); Q.pop (); Q.push (x | | y); } else if (str[i] = = ' N ') {x = Q.top (); Q.pop (); Q.push (!X); } else if (str[i] = = ' C ') {x = Q.top (); Q.pop (); y = Q.top (); Q.pop (); Q.push ((!x) | | y); } else if (str[i] = = ' E ') {x = Q.top (); Q.pop (); y = Q.top (); Q.pop (); Q.push (x = = y); }}}int judge () {for (p=0; p<2; p++) for (q=0; q<2; q++) for (r=0; r<2; r++) for (s=0, s<2; s++) for (t=0; t<2; t++) { In_staCK (); if (Q.top () ==0) return 0; } return 1;} int main () {while (~SCANF ("%s", str)) {GetChar (); if (str[0] = = ' 0 ') break; if (judge ()) {printf ("tautology\n"); } else printf ("not\n"); } return 0;}
POJ 3295-tautology (construction method + stack)