百练/ 北京大学2016研究生推免上机考试(校内)H: S-Trees(二叉树)

题目来源:http://bailian.openjudge.cn/practice/1105/

1105: S-Trees

总时间限制: 1000ms   内存限制:65536kB

描述

A Strange Tree (S-tree) over the variable set Xn ={x1,x2,...,xn} is a binary tree representing a Boolean functionf:{0,1}->{0,1}. Each path of the S-tree begins at the root node and consistsof n+1 nodes. Each of the S-tree's nodes has a depth, which is the amount ofnodes between itself and the root (so the root has depth 0). The nodes withdepth less than n are called non-terminal nodes. All non-terminal nodes havetwo children: the right child and the left child. Each non-terminal node ismarked with some variable xi from the variable set Xn. All non-terminal nodeswith the same depth are marked with the same variable, and non-terminal nodeswith different depth are marked with different variables. So, there is a uniquevariable xi1 corresponding to the root, a unique variable xi2 corresponding tothe nodes with depth 1, and so on. The sequence of the variablesxi1,xi2,...,xin is called the variable ordering. The nodes having depth n arecalled 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 terminalnodes are sufficient to completely describe an S-tree. 
As stated earlier, each S-tree represents a Boolean function f. If you have anS-tree and values for the variables x1,x2,...,xn, then it is quite simple tofind out what f(x1,x2,...,xn) is: start with the root. Now repeat thefollowing: if the node you are at is labelled with a variable xi, thendepending on whether the value of the variable is 1 or 0, you go its right orleft child, respectively. Once you reach a terminal node, its label gives thevalue of the function. 

 

Figure 1: S-trees for the x1 and (x2 or x3) function


On the picture, two S-trees representing the same Boolean function,f(x1,x2,x3)= x1 and (x2 or x3), are shown. For the left tree, the variable ordering is x1,x2, x3, and for the right tree it is x3, x1, x2. 

The values of the variables x1,x2,...,xn, are given as a Variable ValuesAssignment (VVA) 

(x1 = b1, x2 = b2, ..., xn = bn)


with b1,b2,...,bn in {0,1}. For instance, ( x1 = 1, x2 = 1 x3 = 0) would be avalid VVA for n = 3, resulting for the sample function above in the valuef(1,1,0) = 1 and (1 or 0) = 1. The corresponding paths are shown bold in thepicture. 

Your task is to write a program which takes an S-tree and some VVAs andcomputes f(x1,x2,...,xn) as described above.

输入

The input contains the description of several S-trees withassociated VVAs which you have to process. Each description begins with a linecontaining a single integer n, 1 <= n <= 7, the depth of the S-tree. Thisis followed by a line describing the variable ordering of the S-tree. Theformat of that line is xi1 xi2 ...xin. (There will be exactly n differentspace-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 isgiven. There will 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 inwhich they appear in the S-tree, the first character corresponds to theleftmost terminal node of the S-tree, the last one to its rightmost terminalnode. 

The next line contains a single integer m, the number of VVAs, followed by mlines describing them. Each of the m lines contains exactly n characters (eachof which can be 0 or 1), followed by a new-line character. Regardless of thevariable ordering of the S-tree, the first character always describes the valueof x1, the second character describes the value of x2, and so on. So, theline 

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 caseshould not be processed.

输出

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 valueof f(x1,x2,...,xn) for each of the given m VVAs, where f is the functiondefined by the S-tree. 

Output a blank line after each test case.

样例输入

3
x1 x2 x3
00000111
4
000
010
111
110
3
x3 x1 x2
00010011
4
000
010
111
110
0

样例输出

S-Tree #1:
0011

S-Tree #2:
0011

-----------------------------------------------------

解题思路

完全二叉树,用一维数组存储就行了

坑点在于输入数据中连续的01,我想到的解决方案是cin>> s以string形式读入,再遍历string的每个元素转化成int/bool写到数组里

-----------------------------------------------------

代码

 

//H:S-Trees
//总时间限制: 1000ms 内存限制: 65536kB
//描述
//A Strange Tree (S-tree) over the variable set Xn = {x1,x2,...,xn} is a binary tree representing a Boolean function 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 has a depth, which is the amount of nodes between itself and the root (so the root has depth 0). The nodes with depth less than n are called non-terminal nodes. All non-terminal nodes have two children: the right child and the left child. Each non-terminal 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 marked 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 so on. The sequence of the variables xi1,xi2,...,xin is called the variable ordering. The nodes having 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 an S-tree. 
//As stated earlier, each S-tree represents a Boolean function f. If you have an S-tree and values for the variables x1,x2,...,xn, then it is quite simple to find out what f(x1,x2,...,xn) is: start with the root. Now repeat the following: if the node you are at is labelled with a variable xi, 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 x1 and (x2 or x3) function
//
//On the picture, two S-trees representing the same Boolean function,f(x1,x2,x3) = x1 and (x2 or x3), are shown. For the left tree, the variable ordering is x1, x2, x3, and for the right tree it 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 in {0,1}. For instance, ( x1 = 1, x2 = 1 x3 = 0) would be a valid VVA for n = 3, resulting for the sample function above in the value f(1,1,0) = 1 and (1 or 0) = 1. 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 computes f(x1,x2,...,xn) as described above.
//输入
//The input 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, 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 that line is xi1 xi2 ...xin. (There will 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 will 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 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 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 value of x1, the second 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 should not be processed.
//输出
//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 each test case.
//样例输入
//3
//x1 x2 x3
//00000111
//4
//000
//010
//111
//110
//3
//x3 x1 x2
//00010011
//4
//000
//010
//111
//110
//0
//样例输出
//S-Tree #1:
//0011
//
//S-Tree #2:
//0011

#include<fstream>
#include<iostream>
#include<string>
using namespace std;

int main()
{
#ifndef ONLINE_JUDGE
	ifstream fin("tm201601H.txt");
	int n,m,i,j,cnt=1,lnum,index;
	string s;
	while(fin >> n)
	{
		if (n==0)
		{
			break;
		}
		cout << "S-Tree #" << cnt << ":" << endl;
		cnt++;
		int *match = new int[n];
		for (i=0; i<n; i++)
		{
			fin >> s;
			match[s.at(1)-'1'] = i;
		}
		lnum = 1<<n;
		fin >> s;
		bool *leaf = new bool[lnum]();
		for (i=0; i<lnum; i++)
		{
			leaf[i] = s.at(i) - '0';
		}

		fin >> m;
		bool *path = new bool[n]();
		for (i=0; i<m; i++)
		{
			fin >> s;
			for (j=0; j<n; j++)
			{
				path[match[j]] = s.at(j) - '0';
			}
			index = 0;
			for (j=1; j<=n; j++)
			{
				index += (1<<(n-j))*path[j-1];
			}
			cout << leaf[index];
		}
		cout << endl << endl;
		delete[] match;
		delete[] leaf;
		delete[] path;
	}
	fin.close();
#endif
#ifdef ONLINE_JUDGE
	int n,m,i,j,cnt=1,lnum,index;
	string s;
	while(cin >> n)
	{
		if (n==0)
		{
			break;
		}
		cout << "S-Tree #" << cnt << ":" << endl;
		cnt++;
		int *match = new int[n];
		for (i=0; i<n; i++)
		{
			cin >> s;
			match[s.at(1)-'1'] = i;
		}
		lnum = 1<<n;
		cin >> s;
		bool *leaf = new bool[lnum]();
		for (i=0; i<lnum; i++)
		{
			leaf[i] = s.at(i) - '0';
		}

		cin >> m;
		bool *path = new bool[n]();
		for (i=0; i<m; i++)
		{
			cin >> s;
			for (j=0; j<n; j++)
			{
				path[match[j]] = s.at(j) - '0';
			}
			index = 0;
			for (j=1; j<=n; j++)
			{
				index += (1<<(n-j))*path[j-1];
			}
			cout << leaf[index];
		}
		cout << endl << endl;
		delete[] match;
		delete[] leaf;
		delete[] path;
	}
#endif
}


猜你喜欢

转载自blog.csdn.net/da_kao_la/article/details/80316312