Ultraviolet A 712-S-Trees

Source: Internet
Author: User

The questions are as follows:

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, andso on. The sequence of the variablesis 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 whatis: 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 fZ records? Http://www.bkjia.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> vcib0agugznvuy3rpb24gpgltzybzcm9" http://www.2cto.com/uploadfile/Collfiles/20140529/20140529092656200.gif "alt =" $ x_1 \ wedge (x_2 \ vee x_3) $ "align =" MIDDLE "border =" 0 "height =" 34 "width =" 109 ">

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 = 1 X3 = 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 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 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 is X I1 X I2... X I N. (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, 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 :", Where JIs the number of the S-tree. Then print a line that contains the value of for each of the given MVVAs, where FIs 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

As long as you understand the meaning of the question, it is very easy to understand this question. Follow the normal train of thought to build and traverse it, and directly simulate it. Traverse twice, assign values for the first time, and obtain the result for the second time. During input, I ignore X and determine the position of each node according to the subscript of X, then, you can assign a value to the node based on the subscript during the traversal assignment. Place achievements in a loop so that each data does not interfere with each other.

The AC code is as follows:


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.