A strange tree (S-tree) over the variable set is a binary tree representing a Boolean Function
. Each path of the S-tree begins at
RootNode and consistsN+ 1 nodes. Each of the S-tree's nodes has
Depth, Which is the amount of nodes between itself and the root (so the root has depth 0). The nodes with depth less
NAre calledNon-TerminalNodes. All non-terminal nodes have two children:
Right childAndLeft child. Each non-terminal node is marked with some variable
XIFrom the variable setXN. All non-terminal nodes with the same depth are marked with the same variable, and non-terminal nodes with different depth are marked with different variables. So, there is
A unique variableXI1 corresponding to the root, a unique variable
XI2 corresponding to the nodes with depth 1, and so on. The sequence of the variables
Is called
Variable Ordering. The nodes having depthNAre calledTerminalNodes. They have no children and are marked with either 0 or 1. Note that the variable ordering and the distribution of 0's and 1's on terminal nodes are sufficient
Completely describe an S-tree.
As stated earlier, each S-tree represents a Boolean FunctionF. If you have an S-tree and values for the variables
, Then it is quite simple to find out what
Is: start with the root. Now repeat the following: if the node you are at is labeled with a variable
XI, Then depending on whether the value of the variable is 1 or 0, you go its right or left child, respectively. once you reach a terminal node, its label gives the value of the function.
Figure 1: S-trees for the function
On the picture, two S-trees representing the same Boolean function, are shown. For the left
Tree, the variable ordering isX1,X2,
X3, and for the right tree it isX3,X1,
X2.
The values of the variables, are given as
Variable values assignment(Vva)
\ End {displaymath} "src =" http://uva.onlinejudge.org/external/7/712img8.gif "width =" 227 ">
With. For instance ,(
X1 = 1,X2 = 1X3 = 0) wocould be a valid vva
N= 3, resulting for the sample function above in the value. The corresponding paths are shown
Bold in the picture.
Your task is to write a program which takes an S-tree and some vvas and computes
As described abve.
Input
The input file contains the description of several S-trees with associated vvas which you have to process. Each description begins with a line containing a single integer
N, The depth of the S-tree. This is followed by a line describing the variable ordering of the S-tree. The format
Of that line isXI1XI2...XIN. (There will be exactly
NDifferent space-separated strings). So,N= 3 and the Variable Ordering
X3,X1,X2, this line wowould look as follows:
X3 X1 X2
In the next line the distribution of 0's and 1's over the terminal nodes is given. There will be exactly 2NCharacters (each of which can be 0 or 1), followed by the new-line character. The characters are given in the order in which they
Appear in the S-tree, the first character corresponds to the leftmost terminal node of the S-tree, the last one to its rightmost terminal node.
The next line contains a single integerM, The number of vvas, followed
MLines describing them. Each ofMLines contains exactly
NCharacters (each of which can be 0 or 1), followed by a new-line character. Regardless of the Variable Ordering of the S-tree, the first character always describes the value
X1, the second character describes the valueX2, and so on. So, the line
110
Corresponds to the vva (X1 = 1,X2 = 1,
X3 = 0 ).
The input is terminated by a test case startingN= 0. This test case shocould not be processed.
Output
For each S-tree, output the line''S-tree #J:", Where
JIs the number of the S-tree. Then print a line that contains the value
For each of the given
MVvas, whereFIs the function defined by the S-tree.
Output a blank line after each test case.
Sample Input
3x1 x2 x30000011140000101111103x3 x1 x20001001140000101111100
Sample output
S-Tree #1:0011S-Tree #2:0011
The question looks scary. In fact, a full binary tree with a depth less than 8 is provided. Each leaf node corresponds to a value. Each time a path is provided, the value of the corresponding leaf node is output, 0 indicates that the child tree goes to the left, and 1 indicates that the child tree goes to the right. It can be equivalent to binary. The output is saved and output in a group.
# Include <stdio. h>
# Include <math. h>
Int main ()
{Int S, I, j, n, m, a [0, 129], B [8], ANS [20000], sum = 0;
Char X;
While (scanf ("% d", & N), n)
{++ Sum;
For (I = 1; I <= N; I ++)
{Getchar ();
Scanf ("% C % d", & X, & M );
}
For (I = 1; I <= POW (2, n); I ++)
Scanf ("% 1D", & A [I]);
Scanf ("% d", & M );
For (j = 1; j <= m; j ++)
{For (I = 1; I <= N; I ++)
Scanf ("% 1D", & B [I]);
S = 1;
For (I = 1; I <= N; I ++)
If (B [I]) S + = POW (2, n-I );
Ans [J] = A [s];
}
Printf ("s-tree # % d: \ n", sum );
For (I = 1; I <= m; I ++)
Printf ("% d", ANS [I]);
Printf ("\ n ");
}
Return 0;
}