POJ s-trees 1105 (binary tree simulation)

Source: Internet
Author: User
S-trees
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 1501 Accepted: 809

Description a Strange tree (s-tree) over the variable set Xn = {x1,x2,..., Xn} is a binary tree representing A Boolean func tion f:{0,1}->{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 have a depth, which is the amount of nodes between itself and the root (so the root have depth 0) . The nodes with depth less than n is called non-terminal nodes. All non-terminal nodes has a children:the right child and a left child. Each non-terminal node was marked with some variable XI from the variable set Xn. 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 are a unique variable xi1 corresponding to the root, a unique variable xi2 corresponding to the nodes with depth 1, and so on. The sequence of the variables Xi1,xi2,..., xin is called the variable ordering. The nodes has depth n is called terminal nodes. They hAve 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 x1,x2,..., xn, then it's quite simple-to-find out what f (x1,x2,..., xn) Is:start with the root. Now repeat the Following:if the node that is at was labelled with a variable XI and then depending on whether the value of th e 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 X1 and (x2 or x3) function
On the picture, the s-trees representing the same Boolean function,f (x1,x2,x3) = x1 and (x2 or X3), is shown. For the left tree, the variable ordering are x1, x2, X3, and for the right tree it's 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 in {0,1}. For instance, (x1 = 1, x2 = 1 x3 = 0) would bes a valid VVA for n = 3, resulting for the sample function above in the Valu E F (1,1,0) = 1 and (1 or 0) = 1. 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 F (x1,x2,..., xn) as described above.

The input the input 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, 1 <= n <= 7, the depth of the s-tree. This was followed by a line describing the variable ordering of the S-tree. The format of 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

The next line the distribution of 0 ' s and 1 's over the terminal nodes is given. There'll is exactly 2^n characters (each of 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 second Charac ter 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.

Output for each s-tree, output the line "S-tree #j:", where j is the number of the S-tree. Then print a line this 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 each test case.

Sample Input

3
x1 X2 X3
00000111
4
010
111
3
x3 x1 X2
00010011
4
000
010
111
0

Sample Output

S-tree #1:
0011

s-tree #2:
0011
#include <stdio.h>
#include <string.h>
#include <math.h>
Char str[10][2];//branch of a tree  such as (x1  x2  x3) 
Char a[200];//store the number of the end of the tree 
Char b[10];//store The order of the Find points 
Char c[200];//store the last found string of 
int Main () {
	int n,m,i,j,l,k,s=0;
	while (scanf ("%d", &n), N)
	{
		k=0;
		int T=pow (2.0,n);
		for (i=0;i<n;i++)
			scanf ("%s", Str[i]);
		scanf ("%s", a+t);//skill, starting from a[t] Store 
		scanf ("%d", &m);
		for (i=0;i<m;i++)
		{
			l=1;
			scanf ("%s", b);
			for (j=0;j<n;j++)
			{
				if (b[j]== ' 0 ')//Focus:: Find the final corresponding number
					l*=2;
				else
					l=l*2+1;
			}
			C[K++]=A[L];
		}
		C[k]= '///Direct output is finished, the c[k] processing 
		//printf ("S-tree #%d:\n", ++s);
		printf ("%s\n\n", c);
		printf ("S-tree #%d:\n", ++s);//Both of these output methods are for 
		(i=0;i<k;i++)
			printf ("%c", C[i]); 
		printf ("\ n");
	}
	return 0;
}


Related Article

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.