"UVA 712 S-tree" & Two fork Tree _ two fork tree

Source: Internet
Author: User

A Strange tree (s-tree) over the variable set Xn = {x1, x2, ..., Xn} is a binary tree representing a
Boolean function f: {0, 1}
N→{0, 1}. Each path of the s-tree begins at the root node and consists
of n + 1 nodes. Each of the S-tree ' S nodes has a depth which are the amount of nodes between
and the root (so the root has depth 0). The nodes with depth less than n are called
Nodes. All non-terminal nodes have two children:the right child. Each non-terminal
The node is marked with some variable XI from the variable set Xn. All non-terminal nodes with the same
Depth are marked with the same variable, and non-terminal nodes with different depth are with
Different variables. So, there is a unique variable xi1
corresponding to the root, a unique variable xi2
corresponding to the nodes with depth 1, and. The sequence of the variables xi1
, Xi2
,..., Xin
is called the variable ordering. The nodes has depth n are called terminal nodes. 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 a s-tree.
As stated earlier, each s-tree represents a Boolean function f. If you have a s-tree and values for
The variables x1, x2, ..., xn, then it are quite simple to find out what F (x1, x2, ..., xn) Is:start with the
Root. Now repeat the "following:if" node you are are at are labelled with a variable XI
, then depending on
Whether the value of the variable is 1 or 0, for you to 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 x1∧ (X2∨X3)
On the picture, two s-trees representing the same Boolean function, f (x1, x2, x3) = X1∧ (x2∨x3),
are shown. For the "left" tree, the variable ordering are x1, x2, X3, and for the "right" is x3, X1, x2.
The values of the variables x1, x2, ..., xn, are given as a Variable values assignment (VVA)
(x1 = B1, x2 = b2, ..., xn = bn)
With B1, B2, ..., bn∈{0, 1}. 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 F (1, 1, 0) = 1∧ (1∨0) = 1. The corresponding
Paths are shown bold in the picture.
Your task is to write a program which takes a s-tree and some vvas and computes F (x1, x2, ..., xn)
As described above.
Input
The input file contains the description of several s-trees with associated Vvas which your have to
Process. Each description begins with a line containing a single integer n, 1≤n≤7, the depth of the
S-tree. This is followed by a line describing the variable ordering of the S-tree. The format of this line
is Xi1 Xi2
... Xin
. (There'll be exactly n different space-separated strings). So, for n = 3 and the
Variable ordering x3, x1, x2, this line would 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 would be exactly
2
n characters (each of which can be ' 0 ' or ' 1 "), followed by the new-line character. The characters are
Given in the order of which they appear in the S-tree, the the ' the ' character to the corresponds
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 (all of which can be ' 0 ' or ' 1 '), followed
by a new-line character. Regardless of the variable ordering of the S-tree, the the ' the ' of the ' the ', ' character
Describes the value of X1, the second character describes the value of X2, and. 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 should is processed.
Output
For each s-tree, output the line ' S-tree #j: ', where j is the number of the S-tree. Then Print a line
that contains the value of f (x1, x2, ..., xn) for each of the given M Vvas, where F is the function
Defined by the S-tree.
Output a blank line after the test case.

To give the position of N XI, from the top down, as well as the leaf node of this tree, and then the M times query, each query output for the location of the leaf node, each query 1 represents left, 0 represents the right

Train of thought: according to the order of the given XI is 2 ^ (n-i), for each query, directly calculate the weight output corresponding position of the character

AC Code:

#include <cstdio> #include <cmath> #include <deque> #include <queue> #include <vector> #
Include<cstring> #include <algorithm> using namespace std;
const int MAX = 1e5 + 10;
const int INF = 1e9 + 7;
typedef long Long LL;
Char S[max],c[110],sl[max];
int A[max],b[max],o[max];
    int main () {int n,ncase = 0,ok = 0;
        while (~SCANF ("%d", &n), N) {for (int i = 1; I <= n; i++) scanf ("%s", c), a[i] = c[1]-' 0 ';
        LL p = 1;
        for (int i = n; I >= 1; i--) o[a[i]] = p,p *= 2;
        scanf ("%s", s);
        int m;
        scanf ("%d", &m);
            for (int i = 1; I <= m i++) {scanf ("%s", SL);
            int ans = 0;
            for (int j = 0; J < N; j +) if (sl[j] = = ' 1 ') ans + = o[a[j + 1]];
        B[i] = ans;
        printf ("S-tree #%d:\n", ++ncase);
        for (int i = 1; I <= m i++) printf ("%c", S[b[i]);
Puts ("\ 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.