#P7073. [CSP-J 2020] 表达式
[CSP-J 2020] 表达式
Problem Description
Xiao C is keen on studying mathematical logic. One day, he discovered a special kind of logical expression. In this kind of logical expression, all operands are variables, and their values can only be or . The operations are evaluated from left to right. If there are parentheses in the expression, the value of the subexpression inside the parentheses is calculated first. In particular, this kind of expression has and only has the following operations:
- AND operation:
a & b. If and only if both and are , the value of this expression is . In all other cases, the value is . - OR operation:
a | b. If and only if both and are , the value of this expression is . In all other cases, the value is . - NOT operation:
!a. If and only if is , the value of this expression is . In all other cases, the value is .
Xiao C wants to know: given a logical expression and the initial value of each operand in it, after negating the value of one operand, what is the value of the original expression?
To simplify processing the expression, we make the following conventions:
The expression will be given in postfix notation.
The definition of postfix notation is as follows:
- If is an operand, then the postfix form of is itself.
- If is an expression of the form , where is any binary operator, and its precedence is not higher than the operators outside parentheses in and , then the postfix form of is , where and are the postfix forms of and , respectively.
- If is an expression of the form , then the postfix form of is the postfix form of .
Also, for convenience, in the input:
- There is exactly one space on both sides of the AND operator (
&), OR operator (|), and NOT operator (!), but there is no trailing space at the end of the expression. - Each operand is formed by concatenating a lowercase letter
xand a positive integer. The positive integer is the index of the variable. For example,x10means the variable with index . The testdata guarantees that each variable appears exactly once in the expression.
Input Format
The first line contains a string , representing the expression described above.
The second line contains a positive integer , representing the number of variables in the expression. The indices of variables in the expression are .
The third line contains integers. The -th integer represents the initial value of variable .
The fourth line contains a positive integer , representing the number of queries.
The next lines each contain one positive integer, representing the index of the variable to be negated. Note that each modification in a query is temporary, i.e., modifications in previous queries do not affect subsequent queries.
The testdata guarantees that the input expression is valid. The initial values of variables are or .
Output Format
Output lines. Each line contains a or , representing the value of the expression under that query.
x1 x2 & x3 |
3
1 0 1
3
1
2
3
1
1
0
x1 ! x2 x4 | x3 x5 ! & & ! &
5
0 1 0 1 1
3
1
3
5
0
1
1
Hint
Explanation for Sample 1
The infix form of this postfix expression is .
- For the first query, negate the value of . Then the values of the three operands are , , in order. The value of the original expression is .
- For the second query, negate the value of . Then the values of the three operands are , , in order. The value of the original expression is .
- For the third query, negate the value of . Then the values of the three operands are , , in order. The value of the original expression is .
Explanation for Sample 2
The infix form of this expression is $(\operatorname{not}x_1)\operatorname{and}(\operatorname{not}((x_2\operatorname{or}x_4)\operatorname{and}(x_3\operatorname{and}(\operatorname{not}x_5))))$.
Constraints
- For of the testdata, the expression contains only AND operations (
&) or only OR operations (|). - For another of the testdata, , , and .
- For another of the testdata, the initial values of all variables are all or all .
- For of the testdata, , , and .
Here, denotes the length of the string .
Translated by ChatGPT 5