#P6127. [CTSC2000] 逻辑范式
[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 , the truth value of is always the opposite of .
The AND connective, also called conjunction, is a binary connective. For any propositions and , is true if and only if both and are true; otherwise, is false.
The OR connective, also called disjunction, is a binary connective. For any propositions and , is false if and only if both and are false; otherwise, is true.
Below is the truth table of the three basic propositional connectives:
| Proposition | Proposition | |||
|---|---|---|---|---|
| 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 to , ::= 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 has the following truth table:
| Proposition | Proposition | |
|---|---|---|
| T | T | |
| F | ||
| F | T | T |
| F | ||
We can use the expression to represent this implication. The truth table of is:
| Proposition | Proposition | |
|---|---|---|
| T | T | |
| F | ||
| F | T | T |
| F | ||
Thus, we say that the normal form of is .
The completeness theorem tells us that for any -ary logical function , given the truth table of , we can always find a normal form of . A normal form contains only propositions, the three basic connectives, and parentheses. For example, the following is the truth table of a ternary function :
| Proposition | Proposition | Proposition | |
|---|---|---|---|
| T | T | T | T |
| F | |||
| F | T | ||
| F | |||
| F | T | T | |
| F | F | ||
| F | T | ||
| F | |||
That is, when at least two of are true, is true; otherwise, is false.
Of course, the normal form is not unique. Suppose one normal form of is . Then the expression is also a normal form of , and its length is shorter than the former.
Your task is: given the truth table of an -ary function , find a normal form of , and make the normal form as short as possible.
Constraints
Input Format
The first line of the input file is (), indicating that is an -ary function. Then there are lines. Each line is a string of length , containing only characters T and F. The first characters in order represent the values of the propositions, and the last character represents the value of 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 .
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 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 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:
Here, is the full score for that test, is the length of the standard answer, and is the length of your answer.
Thanks to tiger2005 for providing the SPJ.
Translated by ChatGPT 5
京公网安备 11011102002149号