0169. Logical Expression Calculator

Input file name: logcalc.in
Output file name: logcalc.out
Time limit: 6 s
Memory limit: 64 megabytes

Your task is to implement the calculator of logical expressions. The operations allowed are (from highest priority to lowest):

  • `~' (not). Unary operation, which converts true to false and vice versa.
  • `&' (and). Binary operation which is true only if both operands are true.
  • `|' (or). Binary operation which is true if at least one of operands is true.
  • `^' (xor – exclusive or). Binary operation which is true if exactly one of operands is true.

The expression can contain variables and boolean constants 0 (false) and 1 (true). There are 52 different variables allowed: capital and small English letters. Parentheses are also allowed with standard effect.

Input file

The first line of the input contains an expression. The next line contains the number of sets of values of variables for which the expression must be calculated (1 ≤ M ≤ 4063). The next M lines contain 52 numbers each – the values of all variables `A'…`Z' and `a'…`z'. The length of expression does not exceed 50000 characters.

Output file

For each set of input data output the value of expression on a single line. Output 0 if expression is false and 1 otherwise.

Examples:

logcalc.inlogcalc.out
A&B|C 4 1 1 0 0 0 ...47 zeros follow... 1 0 1 0 0 ...47 zeros follow... 1 0 0 0 0 ...47 zeros follow... 0 0 0 0 0 ...47 zeros follow... 1 1 0 0


Source: Petrozavodsk Summer 2003. KOTEHOK's Contest, Sunday, August 31
Author: Andrew Lopatin

Discuss       Submit a solution



Printable version