"UVa 712" s-trees

Source: Internet
Author: User

S-trees
Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld &%llu

Submit Status

Description

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 theRootnode and consists ofN+1 nodes. Each of the S-tree ' S nodes have aDepth, which is the amount of nodes between itself and the root have depth 0). The nodes with depth less thanNis callednon-terminalNodes. All non-terminal nodes has a children:the Right Childand the Left Child. Each non-terminal node was marked with some variablexIFrom the variable setXN. All non-terminal nodes with the same depth is marked with the same variable, and non-terminal nodes with different depth is marked with different variables. So, there is a unique variablexI1corresponding to the root, a unique variablexI2 corresponding to the nodes with depth 1, and so on. The sequence of the variables is called theVariable Ordering. The nodes has depthNis calledTerminalNodes. They has no children and is marked with either 0 or 1. Note that the variable ordering and the distribution of 0 's and 1 ' s on terminal nodes is sufficient to completely describ e an s-tree.

As stated earlier, each s-tree represents a Boolean function f. If you have a s-tree and values for the variables, then it's quite simple-to-find-what-is:start with the root. Now repeat the following:if of the node you were at was labelled with a variable xi, then depending on wheth Er the value of the variable is 1 or 0 and you go it 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, the s-trees representing the same Boolean function, is shown. For the left tree, the variable ordering are x1, x2, x3, and for the right tree it's x 3, x1, x2.

The values of the variables, are given as a Variable values assignment (VVA)


With. For instance, ( x1 = 1, x2 = 1 x3 = 0) would is a valid VVA for N= 3, resulting for the sample function above in the value. The corresponding paths is shown bold in the picture.

Your task is to write a program which takes an s-tree and some vvas and computes as described above.

InputThe input file contains the description of several s-trees with associated Vvas which you has to process. Each description begins with a line containing a single integer N, the depth of the s-tree. This was followed by a line describing the variable ordering of the S-tree. The format of ISx I1x I2 ...x I N. (There'll be exactly NDifferent space-separated strings). So, for N= 3 and the variable ordering x3, x1, x2, this line would look as follows:

X3 X1 X2

The next line the distribution of 0 ' s and 1 's over the terminal nodes is given. There'll is exactly 2n characters (each of the which can is 0 or 1), followed by the new-line character. The characters is 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 integer m, the number of Vvas, followed by m lines describing them. Each of the m lines contains exactly n characters (each of the which can is 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 of x1, the Seco nd character describes the value of x2, 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 starting with n = 0. This test case is should not being processed.

OutputFor each s-tree, output of the line 'S-tree # J:", where JIs the number of the S-tree. Then print a line this contains the value of for each of the given mVvas, where Fis 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

Miguel A. Revilla
2000-02-09The topic looked for a half day ... Understand, in fact, very simple, with a leaf node to build a complete binary tree, and then search by query can be.
#include <cstdio>#include<cstring>using namespacestd;intN, M, Kase;Chars[1<<8], cmd[1<<8];intMain () {Kase=0;  while(SCANF ("%d", &n) = =1&&N) { for(inti =0; I < n; ++i) scanf ("%*s"); scanf ("%s", s); scanf ("%d", &m); printf ("S-tree #%d:\n", ++Kase);  for(inti =0; I < m; ++i) {scanf ("%s", CMD); intU =0;  for(intj =0; J < N; ++j)if(Cmd[j] = ='1') U =2* U +1; ElseU *=2; printf ("%c", S[u]); } printf ("\ n"); }    return 0;}

"UVa 712" s-trees

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.