#P8815. Short-circuit Logical Expression Evaluation
Short-circuit Logical Expression Evaluation
Short-circuit Logical Expression Evaluation
This problem involves evaluating a logical expression consisting of the two constant values 0 (false) and 1 (true), the binary operators & (AND) and | (OR), and parentheses. The usual operator precedence applies: parentheses first, then the & operator, and finally the | operator. Operators of the same precedence are evaluated left‐to‐right.
The twist in this problem is that the evaluation is performed with short-circuit rules as follows:
- For an expression of the form
a&b
: evaluatea
first. Ifa = 0
then the whole expression evaluates to 0 andb
is not evaluated. This counts as one short-circuit of type&
. - For an expression of the form
a|b
: evaluatea
first. Ifa = 1
then the whole expression evaluates to 1 andb
is not evaluated. This counts as one short-circuit of type|
.
Important: If a short-circuited sub-expression is inside another expression that is itself short-circuited, then the inner short-circuit is not counted. For example, in 1|(0&1)
the outer OR short-circuits so (0&1)
is never evaluated and its potential short-circuit is not counted.
Your task is to compute the final value of the expression (either 0 or 1) and report the number of short-circuits that occurred for the two operators (&
and |
) during the evaluation.
inputFormat
The input consists of a single line containing a valid logical expression without any spaces. The expression may contain the characters 0
, 1
, &
, |
and parentheses (
and )
.
You can assume that the expression is non-empty and syntactically correct.
outputFormat
Output a single line containing three integers separated by spaces:
- The final result of the expression (either 0 or 1).
- The number of short-circuits triggered by the AND (
&
) operator. - The number of short-circuits triggered by the OR (
|
) operator.
sample
0|1&0
0 0 0