UVa 10847 - Basic Tautologies

10847 - Basic Tautologies

 * http://acm.uva.es/p/v108/10847.html

Summary
Write a program to check whether given expression is a valid Reverse Polish Notation formula under given constraints, and if it is a tautology.

Explanation
The truth tables given in the problem statement use {0, 1} for the truth values, but it is more natural here to use -1 for false, and +1 for truth. Then the operator "=" becomes just the usual multiplication, and "-" is multiplication by -1.

The expression is a tautology, if it evaluates to +1 regardless of the variables' values, and this can only happen when these two conditions are satisfied:
 * 1) the minus sign occurs an even number of times,
 * 2) each of the variables occurs an even number of times.

Input
3 aa= aa=- ab

Output
tautology formula incorrect