#P7073. Logical Expression Evaluation with Variable Flip

    ID: 20279 Type: Default 1000ms 256MiB

Logical Expression Evaluation with Variable Flip

Logical Expression Evaluation with Variable Flip

In this problem, you are given a logical expression in postfix notation. The expression contains only operands and the following operators:

  • AND (&): For two operands \(a\) and \(b\), \(a \& b\) equals \(1\) if and only if both \(a=1\) and \(b=1\); otherwise, it equals \(0\).
  • OR (|): For two operands \(a\) and \(b\), \(a | b\) equals \(0\) if and only if both \(a=0\) and \(b=0\); otherwise, it equals \(1\).
  • NOT (!): For one operand \(a\), \(!a\) equals \(1\) if \(a=0\) and \(0\) if \(a=1\).

Each operand is a variable of the form xi (e.g. x10), and every variable appears exactly once in the expression. Each variable's value is initially either 0 or 1. Then, a specified variable's value is flipped (i.e. 0 becomes 1 and 1 becomes 0), and the expression is evaluated based on this update.

The expression is given in postfix notation. In postfix notation, every operator follows all of its operands. The tokens are separated by a single space and there is no trailing space at the end of the line. The evaluation should be performed using a stack:

  1. If the token is an operand, push its current value.
  2. If the token is an operator:
    • For !: pop one value, compute its NOT, and push the result.
    • For & and |: pop two values (the first popped is the second operand, the second popped is the first operand), compute the result, and push it.
  3. After processing all tokens, the value on the stack is the output.

Your task is to output the computed result after flipping the designated variable.

inputFormat

The input consists of three lines:

  1. The first line contains the postfix expression. Tokens (operands and operators) are separated by a single space.
  2. The second line contains the initial values of the operands. These values (0 or 1) are given in the order in which the operands appear in the expression, separated by spaces.
  3. The third line contains the name of the operand (e.g. x10) whose value should be flipped.

outputFormat

Output a single integer (either 0 or 1), which is the value of the evaluated expression after the value of the specified operand has been flipped.

sample

x1 x2 &
1 0
x2
1