This page is still under construction.

Parts of this page are still being built. What you see may change.

Beth Tableaux

Time limit2sMemory limit64 MB

Summary
Parse a propositional formula and either report it valid or print the lexicographically smallest falsifying assignment.
Level

Medium6 of 10

Topics
Backtracking, Implementation, Brute force, Recursion
Solved
No attempts yet

Problem

Your program must decide whether a given propositional (boolean) formula EE is valid — that is, whether it evaluates to 1\mathbf{1} (true) under every assignment of boolean values to its variables. If EE is not valid, then it evaluates to 0\mathbf{0} (false) for at least one assignment, and your program must also report such a falsifying assignment.

A naive method tries all 2k2^k assignments of the kk variables, which is hopeless for large kk and unlike the way people actually reason about formulas. The classical Beth tableau (semantic tableau) method searches for a counterexample directly: it keeps two collections of sub-formulas — those we are trying to make true and those we are trying to make false — and repeatedly breaks each compound formula down by its outermost connective. If every branch runs into a contradiction (the same formula forced both true and false, or 0\mathbf{0} forced true, or 1\mathbf{1} forced false), the formula is valid; a branch that survives with no contradiction gives a falsifying assignment (variables forced true get 1\mathbf{1}, those forced false get 0\mathbf{0}). You may use any correct method — only the final answer is checked.

Syntax. Formulas are built as follows, with the connectives listed from the highest priority to the lowest:

  • Constants 1\mathbf{1} (true) and 0\mathbf{0} (false).
  • Variables: the letters A–Z and a–z. They are case-sensitive, so A and a are different variables.
  • Parentheses: if EE is a formula, then so is (E)(E).
  • Negation: ¬E\neg E.
  • Conjunction E1∧E2∧⋯∧EnE_1 \wedge E_2 \wedge \cdots \wedge E_n, evaluated left to right: E1∧E2∧E3=(E1∧E2)∧E3E_1 \wedge E_2 \wedge E_3 = (E_1 \wedge E_2) \wedge E_3.
  • Disjunction E1∨E2∨⋯∨EnE_1 \vee E_2 \vee \cdots \vee E_n, also evaluated left to right.
  • Implication E1⇒E2E_1 \Rightarrow E_2, evaluated right to left: E1⇒E2⇒E3E_1 \Rightarrow E_2 \Rightarrow E_3 means E1⇒(E2⇒E3)E_1 \Rightarrow (E_2 \Rightarrow E_3).
  • Equivalence E1≡E2≡⋯≡EnE_1 \equiv E_2 \equiv \cdots \equiv E_n, defined as (E1≡E2)∧(E2≡E3)∧⋯∧(En−1≡En)(E_1 \equiv E_2) \wedge (E_2 \equiv E_3) \wedge \cdots \wedge (E_{n-1} \equiv E_n).

The connectives have their usual truth tables: ¬\neg is logical NOT, ∧\wedge is AND, ∨\vee is OR, A⇒BA \Rightarrow B equals ¬A∨B\neg A \vee B, and A≡BA \equiv B is true exactly when AA and BB have the same value.

Input

A single line containing the formula, written as a string of tokens: 0, 1, the letters A–Z and a–z, (, ), ~, &, |, =>, and =. The last five tokens stand for ¬\neg, ∧\wedge, ∨\vee, ⇒\Rightarrow, and ≡\equiv respectively. Tokens may be separated by any number of spaces. The line contains at most 10001000 characters, and the formula is guaranteed to be syntactically correct.

Output

Print exactly one line.

  • If the formula is valid, print true.
  • Otherwise print false. If the formula contains at least one variable, append : followed by the lexicographically smallest falsifying assignment. Sort the variables in ascending order by character code (so uppercase letters come before lowercase), and among all assignments that make the formula equal to 0\mathbf{0} choose the one whose bit vector is smallest, reading the first variable as the most significant bit and taking 0<10 < 1. Write the assignment as var=value pairs joined by , (a comma and a space), for example false: A=0, B=1. If the formula has no variables at all, print just false.

Examples12

  1. Example 1

    Input
    0
    
    Expected output
    false
    
  2. Example 2

    Input
    1
    
    Expected output
    true
    
  3. Example 3

    Input
    A
    
    Expected output
    false: A=0
    
  4. Example 4

    Input
    A|B => A&B
    
    Expected output
    false: A=0, B=1
    
  5. Example 5

    Input
    A&B => A|B
    
    Expected output
    true
    
  6. Example 6

    Input
    r=>Y
    
    Expected output
    false: Y=0, r=1
    
  7. Example 7

    Input
    R=r
    
    Expected output
    false: R=0, r=1
    
  8. Example 8

    Input
    (A=>B)&(B=>C)=>(A=>C)
    
    Expected output
    true
    
  9. Example 9

    Input
    (A=>B)=>(~B=>~A)
    
    Expected output
    true
    
  10. Example 10

    Input
    (A=>B)=>(~A=>~B)
    
    Expected output
    false: A=0, B=1
    
  11. Example 11

    Input
    (A=a)&(B=b)=>(A&B=a&b)
    
    Expected output
    true
    
  12. Example 12

    Input
    K|~i|t|t|~e|~n
    
    Expected output
    false: K=0, e=1, i=1, n=1, t=0