eolymp
bolt
Try our new interface for solving problems
Problems

Set Operations

Set Operations

In this problem, you have to perform operations with sets according to the given expression. Consider an expression involving three sets \textbf{A}, \textbf{B} and \textbf{C} and three set operations: union, intersection and complement. For the purposes of this problem, sets can only consist of numbers \{\textbf{1}, \textbf{2}, ... , \textbf{n}\} for some fixed integer \textbf{n}. A union of sets \textbf{X} and \textbf{Y} is a set which contains all numbers present in at least one of the sets \textbf{X} and \textbf{Y}. An intersection of sets \textbf{X} and \textbf{Y} is a set which contains all numbers present in both of the sets \textbf{X} and \textbf{Y} . A complement of set \textbf{X} is a set which contains all numbers from the range \{\textbf{1}, \textbf{2}, ... , \textbf{n}\} which are not in \textbf{X}. For example, if \textbf{n} = \textbf{5}, \textbf{X} = \{\textbf{3}, \textbf{4}\} and \textbf{Y} = \{\textbf{1}, \textbf{3}\}, the union of \textbf{X} and \textbf{Y} is \{\textbf{1}, \textbf{3}, \textbf{4}\}, their intersection is \{\textbf{3}\}, and complement of \textbf{X} is \{\textbf{1}; \textbf{2}; \textbf{5}\}. An expression is recursively defined as follows: \begin{itemize} \item \textbf{A}, \textbf{B} and \textbf{C} are expressions denoting the three given sets \textbf{A}, \textbf{B} and \textbf{C} respectively. \item If \textbf{E} is an expression, ~\textbf{E} and (\textbf{E}) are also expressions. \item If \textbf{E} and \textbf{F} are expressions, \textbf{E }| \textbf{F} and \textbf{E} & \textbf{F} are also expressions. \end{itemize} Here, ~\textbf{X} is the complement of set \textbf{X}, \textbf{X} | \textbf{Y} is the union of sets \textbf{X} and \textbf{Y} , and \textbf{X} & \textbf{Y} is the intersection of sets \textbf{X} and \textbf{Y}. Complement is evaluated before intersection, which in turn is evaluated before union. Parentheses play the usual role of prioritizing operations. So, for example, an expression \textbf{A} | ~\textbf{B} & \textbf{C} is evaluated in the same way as \textbf{A} | ((~\textbf{B}) & \textbf{C}). You are given one expression and a number of triples of sets \textbf{A}, \textbf{B} and \textbf{C}. Find and output the value of the expression for each of the given triples. \InputFile The first line contains the expression. Its length is from \textbf{1} to \textbf{300 000} characters. It is guaranteed that the expression conforms with the recursive definition above. There are no spaces on the first line. The second line contains two integers \textbf{n} and \textbf{t} (\textbf{1} ≤ \textbf{n} ≤ \textbf{20}, \textbf{1} ≤ \textbf{t} ≤ \textbf{10 000}) separated by a space: the number of elements and the number of triples of sets. Each of the next \textbf{t} lines describes a triple of sets \textbf{A}, \textbf{B} and \textbf{C} in that order. A set is denoted by a list of integers contained in that set in strictly ascending order followed by a zero. Numbers are separated by one or more spaces. \OutputFile For each of the \textbf{t} given triples of sets, output a single line containing a single set: the result of evaluating the expression for the given \textbf{A}, \textbf{B} and \textbf{C}. A set must be written as a list of integers contained in that set in strictly ascending order followed by a zero. Separate numbers by one space.
Time limit 1 second
Memory limit 64 MiB
Input example #1
A|~B&C
5 2
1 3 0 3 4 0 2 3 5 0
0 1 3 5 0 0
Output example #1
1 2 3 5 0
0

Example description: The expression is evaluated as A | ((~B) & C). For the first triple ~B = {1, 2, 5}, ~B & C = {2, 5} and the whole expression evaluates to the set {1, 2, 3, 5}. For the second triple, ~B = {2, 4}, ~B & C is empty and the result is also an empty set.

Source 2014 Petrozavodsk, Ivan Kazmenko Contest, August 22, Problem I