This page is still under construction.

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

Boolean Logic

Time limit1sMemory limit128 MB

Summary
Parse a fully parenthesized proposition formula, then print a truth table with each subformula's value placed at its symbol or operator column.
Level

Medium6 of 10

Topics
Implementation, Recursion, String, Simulation
Solved
No attempts yet

Problem

A proposition is a logical formula built from proposition symbols and connective operators. Propositions are defined recursively by the following rules.

  1. Every proposition symbol (here, a single lower-case letter, for example a or z) is a proposition.
  2. If PP is a proposition, then (!PP) is a proposition, and PP is a direct subformula of it.
  3. If PP and QQ are propositions, then (PP&QQ), (PP|QQ), (PP-->QQ), and (PP<->QQ) are propositions, and PP and QQ are direct subformulas of each of them.
  4. Nothing else is a proposition.

The operators !, &, |, -->, and <-> denote negation, conjunction, disjunction, implication, and equivalence, respectively. A proposition PP is a subformula of a proposition RR if P=RP = R, or if PP is a direct subformula of some proposition QQ and QQ is a subformula of RR.

Now take a proposition PP and assign a boolean value (00 or 11) to every proposition symbol that occurs in PP. This induces a boolean value for every subformula of PP according to the standard semantics of the operators:

negationconjunctiondisjunctionimplicationequivalence
!0=10&0=00|0=00-->0=10<->0=1
!1=00&1=00|1=10-->1=10<->1=0
1&0=01|0=11-->0=01<->0=0
1&1=11|1=11-->1=11<->1=1

In this way a value for PP is obtained. This value depends on the chosen assignment. If PP contains nn distinct proposition symbols, there are 2n2^n different assignments. To examine all of them we use a truth table.

A truth table has one line per assignment (that is, 2n2^n lines in total). Each line lists the values of all subformulas under the corresponding assignment. When a subformula is a proposition symbol, its value is aligned with that symbol; otherwise its value is aligned with the center of the operator.

Input

The input contains several test cases, one per line. Each line denotes a proposition and may contain any number of spaces between its characters. The input ends immediately after the newline that follows the last test case.

Output

For each test case, produce a truth table for the given proposition. Begin the truth table by repeating the input line exactly. Then evaluate the proposition and all of its subformulas for every assignment of boolean values to its symbols, and output one line per assignment. Each such line must have the same length as the corresponding input line and may contain only spaces and the characters 0 and 1, with every subformula's value placed in the column described above. Output an empty line after each test case.

Let s1,…,sns_1, \ldots, s_n be the proposition symbols of the given proposition sorted in alphabetical order. Then every assignment that gives 00 to s1s_1 must come before every assignment that gives 11 to s1s_1. Within each of those blocks, every assignment that gives 00 to s2s_2 must come before every assignment that gives 11 to s2s_2, and so on (so sns_n changes fastest).

Examples3

  1. Example 1

    Input
    ((b --> a) <-> ((! a) --> (! b)))
      ((y &  a)   -  ->(c |c))
    
    Expected output
    ((b --> a) <-> ((! a) --> (! b)))
      0  1  0   1    1 0   1   1 0   
      1  0  0   1    1 0   0   0 1   
      0  1  1   1    0 1   1   1 0   
      1  1  1   1    0 1   1   0 1   
    
      ((y &  a)   -  ->(c |c))
        0 0  0       1  0 00  
        1 0  0       1  0 00  
        0 0  0       1  1 11  
        1 0  0       1  1 11  
        0 0  1       1  0 00  
        1 1  1       0  0 00  
        0 0  1       1  1 11  
        1 1  1       1  1 11  
    
  2. Example 2

    Input
    a
    
    Expected output
    a
    0
    1
    
  3. Example 3

    Input
    (!a)
    
    Expected output
    (!a)
     10 
     01