Beth Tableaux | 프로그래밍의 벗 PivotOJ
PivotOJ

Beth Tableaux

시간 제한: 2000ms메모리 제한: 64MB출처: NEERC Northern Subregional 2004BOJ 7460

문제

Your program has to check the validity of a given logical formula. If the formula EE is invalid (i.e. evaluates to 0\mathbf{0} for some assignment of boolean values to the variables), you’ll also have to find such an assignment of values.

Usually such tasks are solved just by processing all possible assignments of values to the variables occurring in EE; if there are kk such variables, this requires considering 2k2^k such assignments. Of course, this is almost impossible for large values of kk. Besides, this process is much different from the way people usually check (i.e. prove) logical formulae.

In this task you are to implement a natural way of checking validity of logical formulae based on the so-called Beth tableaux. This way of checking validity of formulae models to a certain extent the process of finding proofs or contrary instances used by humans, at least, a short rigorous proof can be usually obtained quite immediately from constructed Beth tableaux.

Let us fix the syntax of logical formulae considered:

  • Constants 1\mathbf{1} and 0\mathbf{0};
  • Variables — letters from A to Z and from a to z;
  • Parentheses — if EE is a formula, then (E)(E) is another;
  • Negation — ¬E\neg E is a formula for any formula EE;
  • Conjunction — E1E2EnE_1 \wedge E_2 \wedge \cdots \wedge E_n. Note that the conjunction is evaluated from left to right: E1E2E3=(E1E2)E3E_1 \wedge E_2 \wedge E_3 = (E_1 \wedge E_2) \wedge E_3.
  • Disjunction — E1E2EnE_1 \vee E_2 \vee \cdots \vee E_n. The same remark applies.
  • Implication — E1E2E_1 \Rightarrow E_2. Unlike the previous two operations it is evaluated from right to left: E1E2E3E_1 \Rightarrow E_2 \Rightarrow E_3 means E1(E2E3)E_1 \Rightarrow (E_2 \Rightarrow E_3).
  • Equivalence — E1E2EnE_1 \equiv E_2 \equiv \cdots \equiv E_n. This expression is by definition computed as follows: (E1E2)(E2E3)(En1En)(E_1 \equiv E_2) \wedge (E_2 \equiv E_3) \wedge \cdots \wedge (E_{n-1} \equiv E_n).

The operations are listed from the highest priority to the lowest.

Let us define the principal operation of a compound formula EE as the operation in EE which is performed last (of course, formulae consisting just of one constant or variable have no principal operation). Also let us suppose that our formula does not contain equivalence operations since we can always replace E1E2E_1 \equiv E_2with (E1E2)(E2E1)(E_1 \Rightarrow E_2) \wedge (E_2 \Rightarrow E_1).

Now we define a Beth tableau to be a table with two columns with some formulae written in them. The order formulae are written in the columns and the number of times a formula appears in a column is irrelevant: we can assume for example that all formulae in a column are different and that they are ordered lexicographically.

Informally the left column corresponds to formulae which we want to be true and the right column to those which we want to be false; the whole process can be considered then as an attempt to produce a contrary instance to given formula EE. A Beth tableau is said to be contradictory if some formula EE_* occurs in both columns of this tableau, or if 0\mathbf{0} occurs in the left column, or if 1\mathbf{1} occurs in the right column.

At every step of the process we have a collection of Beth tableaux. We start with a single Beth tableau with empty left column and the right column containing just the given formula EE. A step of the process consists in choosing a compound formula CC from a non-contradictory Beth tableau from the collection and applying a rule chosen by the principal operation of CC and by the column (left or right) in which CC is situated. As a result some formulae are added to the left or right columns of this Beth tableau (or both); of course, if these formulae that we want to add are already there the Beth tableau won’t change, so in this case we say that the corresponding rule is inapplicable. There are also some rules that make two copies of the original Beth tableau and add some formulae into one of these copies and some other into the second one. In this case we say that the rule is inapplicable if both Beth tableaux thus obtained were already in the collection before applying the rule.

The process stops when no rule is applicable. Note that all formulae occurring in all Beth tableaux constructed will be sub-formulae of the original formula, so there are only finitely many Beth tableaux that can be obtained and thus only finitely many collections of Beth tableaux can be obtained. Therefore the process will have to stop at some point. If at this point all Beth tableaux in the collection are contradictory, the original formula EE has been proved to be valid. Otherwise if τ\tau is a non-contradictory Beth tableau from the collection, any variable xx occurring in EE must occur in exactly one column of τ\tau (it cannot occur in both columns since τ\tau is not contradictory; it must occur in at least one column since otherwise a rule would be applicable to the shortest formula in τ\tau containing xx). Then let us assign 1\mathbf{1} to all the variables from the left column and 0\mathbf{0} to the variables from the right column of τ\tau. This would give us a contrary instance to EE, i.e. an assignment of values to variables for which our original formula EE evaluates to 0\mathbf{0}.

Now let us list all possible rules. It has been already explained that a rule is defined by the principal operation * of a formula CC and the column (left or right) in which CC was found. We shall label these rules by l*_l and r*_r where * is the operation (one of \wedge, \vee, \Rightarrow or ¬\neg). If * is binary, we shall assume that C=ABC = A * B; otherwise we assume C=¬AC = \neg A.

Let us list the rules that do not increase the number of tableaux (i.e. they just add some formulae to the columns of selected Beth tableau):

l:AB(A)(B)   r:AB(A)(B)   r:(A)AB(B)\wedge_l : \left. \begin{matrix} A \wedge B &\\ \cdots &\\ (A) & \\ (B) & \end{matrix} \right\vert \begin{matrix} & \cdots & \\ & & \\ & & \\ & & \end{matrix} ~~~ \vee_r : \begin{matrix} \cdots & \\ & \\ & \\ & \end{matrix} \left\vert \begin{matrix} & A \wedge B & \\ & \cdots & \\ & (A) & \\ & (B) & \end{matrix} \right. ~~~ \Rightarrow_r : \begin{matrix} & \cdots & \\ & (A) & \\ & & \end{matrix} \left\vert \begin{matrix} & A \Rightarrow B \\ & \cdots \\ & (B) \end{matrix} \right.

¬l:¬A(A)   ¬r:(A)¬A\neg_l : \begin{matrix} & \neg A & \\ & \cdots & \end{matrix} \left\vert \begin{matrix} & \cdots & \\ & (A) & \end{matrix} \right. ~~~ \neg_r : \begin{matrix} & \cdots & \\ & (A)& \end{matrix} \left\vert \begin{matrix} & \neg A& \\ & \cdots & \end{matrix} \right.

This means the following: for example, if we apply the rule r\Rightarrow_r to the formula ABA \Rightarrow B in the right column of a Beth tableau, then we have to add formula AA to the left column and BB to the right column of this tableau.

The next rules produce two Beth tableaux from one. A similar notation is used, but two Beth tableaux are shown for each rule:

r:AB(A) AB(B)   l:AB(A) AB(B)\wedge_r : \left. \begin{matrix} \cdots &\\ &\\ & \end{matrix} \right\vert \begin{matrix} & A \wedge B & \\ & \cdots & \\ & (A) & \end{matrix} ~ \left. \begin{matrix} \cdots &\\ &\\ & \end{matrix} \right\vert \begin{matrix} & A \wedge B & \\ & \cdots & \\ & (B) & \end{matrix} ~~~ \vee_l : \left. \begin{matrix} A \vee B & \\ \cdots & \\ (A) & \end{matrix} \right\vert \begin{matrix} & \cdots & \\ & & \\ & & \end{matrix} ~ \left. \begin{matrix} A \vee B & \\ \cdots & \\ (B) & \end{matrix} \right\vert \begin{matrix} & \cdots & \\ & & \\ & & \end{matrix}

l:AB(A) AB(B)\Rightarrow_l : \left. \begin{matrix} A \Rightarrow B &\\ \cdots & \\ & \end{matrix} \right\vert \begin{matrix} & \cdots & \\ & (A) & \\ & & \end{matrix} ~ \left. \begin{matrix} A \Rightarrow B&\\ \cdots &\\ (B) & \end{matrix} \right\vert \begin{matrix} & \cdots & \\ & & \\ & & \end{matrix}

These three rules are interpreted as follows: if we apply for example l\vee_l to the formula ABA \vee B in the left column of a Beth tableau τ\tau, we have to replace τ\tau with two copies of itself τ\tau ' and \tau ' ', and then add AA to the left column of τ\tau ' and BB to the left column of \tau ' '.

입력

The only line of input contains the formula represented as a string consisting of tokens ‘0’, ‘1’, ‘A’. . . ‘Z’, ‘a’. . . ‘z’, ‘(’, ‘)’, ‘~’, ‘&’, ‘|’, ‘=>’, ‘=’. The last five tokens stand for ¬\neg, \wedge, \vee , \Rightarrow and \equiv respectively. Tokens can be separated by an arbitrary number of spaces. The line will contain at most 1 000 characters. The formula in the file will be syntactically correct.

출력

The output file must contain exactly one line. Output just “true” if the formula is valid or “false” followed by an assignment of variables that invalidates the formula. List the variables in lexicographical order. Adhere to the sample output as strictly as possible.

예제

예제 1

입력
0
출력
false

예제 2

입력
(A=>B)=>(~A=>~B)
출력
false: A=0, B=1

예제 3

입력
(A=a)&(B=b)=>(A&B=a&b)
출력
true

예제 4

입력
K|~i|t|t|~e|~n
출력
false: K=0, e=1, i=1, n=1, t=0

예제 5

입력
1
출력
true

예제 6

입력
A
출력
false: A=0

예제 7

입력
A|B => A&B
출력
false: A=1, B=0

예제 8

입력
A&B => A|B
출력
true

예제 9

입력
r=>Y
출력
false: Y=0, r=1

예제 10

입력
R=r
출력
false: R=0, r=1

예제 11

입력
(A=>B)&(B=>C)=>(A=>C)
출력
true

예제 12

입력
(A=>B)=>(~B=>~A)
출력
true
코드를 제출하려면 로그인하세요.