Editorial for WC '16 Contest 2 S1 - Most Illogical
Submitting an official solution before solving the problem yourself is a bannable offence.
Any boolean expression in this problem can be thought of as consisting of one or more "clauses", where each clause consists of one or more literals and
'ed together, with all of the clauses then or
'ed together. For example, the expression A or B and C and D or E
consists of clauses, A
, B and C and D
, and E
.
If at least one of the literals in a clause is false
, then the result of the whole clause is definitely false
, due to the properties of the and
operator. Otherwise, if at least one of the literals is unknown
, then the result of the whole clause is unknown
(it could be false
if any unknown literals are false
, or it could be true
if all unknown literals are true
). Finally, if all of the literals are true
, then the result of the whole clause is true
.
Similar logic can be applied to the whole expression, though inverted due to properties of the or
operator. If at least one of the clauses is true
, then the result of the whole expression is true
. Otherwise, if at least one of the clauses is unknown
, then the result of the whole expression is unknown
. Finally, if all of the clauses are false
, then the result of the whole expression is false
.
With these facts in mind, we can process the expression from left to right, while keeping track of the result of the ongoing clause, and the result of the whole expression. When a literal is processed, the result of the ongoing clause should be updated accordingly (for example, if a false
literal is encountered, then the result of the ongoing clause should be set to false
). Similarly, when the end of a clause is reached (which happens after the last literal or when an or
operator is encountered), the result of the whole expression should be updated according to the result of that clause, and then a new clause should be started.
Comments