Ultraviolet A 712-S-trees

Source: Internet
Author: User
 
S-trees

A strange tree (S-tree) over the variable set is a binary tree representing a Boolean function. Each path of the S-tree begins atRootNode and consistsN+ 1 nodes. Each of the S-tree's nodes hasDepth, Which is the amount of nodes between itself and the root (so the root has depth 0). The nodes with depth lessNAre calledNon-TerminalNodes. All non-terminal nodes have two children:Right childAndLeft child. Each non-terminal node is marked with some variableXIFrom 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 variableXI2 corresponding to the nodes with depth 1, and so on. The sequence of the variables is calledVariable 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 to 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 variableXI, 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 asVariable values assignment(Vva)


With. For instance ,(X1 = 1,X2 = 1X3 = 0) wocould be a valid vvaN= 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 computesas described above.

 

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 integerN, 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 exactlyNDifferent space-separated strings). So,N= 3 and the Variable OrderingX3,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, followedMLines describing them. Each ofMLines contains exactlyNCharacters (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 valueX1, 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:", WhereJIs the number of the S-tree. Then print a line that contains the value of for each of the givenMVvas, whereFIs thefunction 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
# Include <iostream> # include <stack> # include <cstring> # include <cstdio> # include <string> # include <algorithm> # include <queue> # include <set> # include <map> # include <fstream> # include <stack> # include <list> # include <sstream> # include <cmath> using namespace STD; /**/# define MS (ARR, Val) memset (ARR, Val, sizeof (ARR )) # define N 10 # define INF 0x3fffffff # define Vint vector <int> # define setint set <int> # define mint Map <int, int> # define lint list <int> # define Sch stack <char> # define qch queue <char> # define Sint stack <int> # define qint queue <int> int stree [n]; char term [1000]; char vva [N];/* It is relatively simple to understand the question. For a vva, find the value of the corresponding leaf node. In vva, 0 indicates to go to the left subtree, and 1 indicates to look for the right subtree. For example, in the question, right tree it is X3, x1, x2; The vva is 1 0, that is, if X3 = 0, X1 = 1, X2 = 2, it is first left, right, and then right, it is the answer 1 */INT Expo (int A, int N) // a ^ n {int ans = 1; while (n) {If (N & 1) {ans * = A;} A * =; N >>= 1 ;}return ans;} void bin_search (int I, Int J, int p) // Binary Search {if (I> = J) {putchar (term [J]); return;} If (vva [stree [p] = '0') bin_search (I, (I + J)/2, P + 1); else bin_search (I + J)/2 + 1, J, p + 1);} int main () {int N, Len, I, m, cases = 1; string t; while (scanf ("% d", & N), n) {I = 1; CIN> T; Len = T. length ()-1; stree [I] = T [Len]-'0'; for (I ++; I <= N; I ++) {CIN> T; stree [I] = T [Len]-'0';} Len = Expo (2, n); scanf ("% s ", term + 1); scanf ("% d", & M); printf ("s-tree # % d: \ n", cases ++); While (M --) {scanf ("% s", vva + 1); bin_search (1, Len, 1);} printf ("\ n");} return 0 ;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.