#P6127. [CTSC2000] 逻辑范式

    ID: 5002 远端评测题 1000ms 128MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>搜索贪心2000WC/CTSC/集训队Special Judge

[CTSC2000] 逻辑范式

Description

Logic is a science that studies propositions and their truth values. A proposition is usually represented by a lowercase letter, and the value of any proposition is either true or false. We use T to denote that a proposition is true, and F to denote that a proposition is false.

Operators between propositions are called propositional connectives, or simply connectives. The three most basic connectives in a logic system are NOT, AND, and OR. For simplicity, we use !! for NOT, &\& for AND, and | for OR.

The NOT connective is a unary connective. For any proposition pp, the truth value of !p!p is always the opposite of pp.

The AND connective, also called conjunction, is a binary connective. For any propositions pp and qq, p&qp\&q is true if and only if both pp and qq are true; otherwise, p&qp\&q is false.

The OR connective, also called disjunction, is a binary connective. For any propositions pp and qq, pqp|q is false if and only if both pp and qq are false; otherwise, pqp|q is true.

Below is the truth table of the three basic propositional connectives:

Proposition pp Proposition qq !p!p p&qp\&q pqp|q
T T F T T
F F
F T T
F F

A logical expression is composed of propositions, connectives, and parentheses. It is defined as follows:

<Expression> ::=<Conjunction> or <Disjunction> or <Negation> or (<Expression>) or <Atom>

<Conjunction> ::=<Expression>&<Expression>

<Disjunction> ::=<Expression>|<Expression>

<Negation> ::=!<Expression>

<Atom> ::=<Proposition>

Note: here, is a lowercase letter from aa to zz, ::= means “is defined as”, and or means “or”.

The evaluation of an expression is the process of performing propositional operations according to the truth values of propositions in the expression and the truth tables. The operator precedence is ()(), !!, &\&, and |. &\& and | have the same precedence. Operators with the same precedence are evaluated from left to right.

An AND-OR-NOT logical normal form (hereinafter referred to as “normal form”) is a logical expression that matches a specific truth table. For example, in standard logic, the implication operator \rightarrow has the following truth table:

Proposition pp Proposition qq pqp \rightarrow q
T T
F
F T T
F

We can use the expression !pq!p|q to represent this implication. The truth table of !pq!p|q is:

Proposition pp Proposition qq !pq!p|q
T T
F
F T T
F

Thus, we say that the normal form of pqp \to q is !pq!p|q.

The completeness theorem tells us that for any nn-ary logical function AA, given the truth table of AA, we can always find a normal form of AA. A normal form contains only propositions, the three basic connectives, and parentheses. For example, the following is the truth table of a ternary function A(p,q,r)A(p,q,r):

Proposition pp Proposition qq Proposition rr A(p,q,r)A(p,q,r)
T T T T
F
F T
F
F T T
F F
F T
F

That is, when at least two of p,q,rp, q, r are true, AA is true; otherwise, AA is false.

Of course, the normal form is not unique. Suppose one normal form of AA is (p&q&r)(!p&q&r)(p&!q&r)(p&q&!r)(p\&q\&r)|(!p\&q\&r)|(p\&!q\&r)|(p\&q\&!r). Then the expression (p&q)(q&r)(p&r)(p\&q)|(q\&r)|(p\&r) is also a normal form of AA, and its length is shorter than the former.

Your task is: given the truth table of an nn-ary function AA, find a normal form of AA, and make the normal form as short as possible.

Constraints

Input Format

The first line of the input file is nn (n6n \leq 6), indicating that AA is an nn-ary function. Then there are 2n2^n lines. Each line is a string of length n+1n+1, containing only characters T and F. The first nn characters in order represent the values of the nn propositions, and the last character represents the value of AA under this assignment.

Output Format

The output file contains one line: the normal form you provide. The propositions are represented in order by lowercase letters a,b,c,d,a, b, c, d, \ldots .

3
TTTT
TTFT
TFTT
TFFF
FTTT
FTFF
FFTF
FFFF

a&b|(a|b&c)

Hint

Scoring:

  • If the normal form you give is incorrect, or you do not produce a solution within the time limit, you get 00 points for that test.
  • If the length of your normal form is less than or equal to the length of the standard answer, you get full points for that test.
  • If the length of your normal form is at least twice the length of the standard answer, you get 00 points for that test.
  • If the length of your normal form is greater than the length of the standard answer and less than twice the length of the standard answer, the score is computed as:
Score=FullScore×2LstdLLstdScore=FullScore \times \frac{2L_{std}-L}{L_{std}}

Here, FullScoreFullScore is the full score for that test, LstdL_{std} is the length of the standard answer, and LL is the length of your answer.

Thanks to tiger2005 for providing the SPJ.

Translated by ChatGPT 5