Tautology
InterviewTime limit1sMemory limit128 MB
Given propositional formulas in Polish notation, decide for each whether it is a tautology by parsing and evaluating it over every truth assignment.
- Level
Medium4 of 10
- Topics
- String, Recursion, Brute force, Implementation
- Solved
- No attempts yet
Problem
WFF 'N PROOF is a logic game played with dice. Each die has six faces showing some subset of the 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, andtare WFFs.- If
wis a WFF, thenNwis a WFF. - If
wandxare WFFs, thenKwx,Awx,Cwx, andEwxare WFFs.
The meaning of a WFF is defined as follows:
p,q,r,s, andtare logical variables that may take the value 0 (false) or 1 (true).K,A,N,C, andEmean and, or, not, implies, and equals, as defined in the truth table below.
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 value 0 when p=0 and q=1.
Determine whether or not a given WFF is a tautology.
Input
The 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 only 0 follows the last case.
Output
For each test case, output a single line containing tautology or not, as appropriate.