描述
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.
变量集 Xn = { x1,x2,... ,Xn }上的奇异树(s- 树)是表示布尔函数 f: {0,1}-> {0,1}的二叉树。S 树的每条路径从根节点开始,由 n + 1个节点组成。每个 S 树的节点都有一个深度,即它自己和根之间的节点数(因此根的深度为0)。深度小于 n 的节点称为非终端节点。所有非终端节点都有两个子节点: 右子节点和左子节点。每个非终端节点都用变量 set Xn 中的某个变量 xi 标记。所有具有相同深度的非终端节点用相同的变量标记,而具有不同深度的非终端节点用不同的变量标记。因此,有一个唯一的变量 xi1对应于根,一个唯一的变量 xi2对应于深度为1的节点,依此类推。变量 xi1,xi2,... ,xin 的序列称为变量排序。深度为 n 的节点称为终端节点。它们没有子女,并且标记为0或1。请注意,变量排序以及0和1在终端节点上的分布足以完全描述 S 树。如前所述,每个 s- 树代表一个布尔函数 f。如果您有一个 S- 树和变量 x1,x2,... ,xn 的值,那么很容易找到 f (x1,x2,... ,xn)是什么: 从根开始。现在重复以下步骤: 如果您所在的节点用变量 xi 标记,那么根据变量的值是1还是0,您将分别使用它的右子节点或左子节点。一旦到达终端节点,它的标签就会给出函数的值。
对于左边的树,变量的顺序是 x1,x2,x3,对于右边的树,变量的顺序是 x3,x1,x2。变量 x1,x2,... ,xn 的值作为变量值赋值(VVA)(x1 = b1,x2 = b2,... ,xn = bn) ,b1,b2,... ,bn 在{0,1}中。例如,(x1 = 1,x2 = 1 x3 = 0)对于 n = 3将是一个有效的 VVA,导致上面的样本函数的值为 f (1,1,0) = 1和(1或0) = 1。相应的路径在图中以粗体显示。您的任务是编写一个程序,该程序使用一个 S- 树和一些 VVA,并如上所述计算 f (x1,x2,... ,xn)。
输入
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.
输入包含几个 S- 树的描述,以及必须处理的相关 VVA。每个描述都以一行开始,其中包含一个单一整数 n,1 < = n < = 7,即 S- 树的深度。接下来是一行描述 S- 树的变量顺序。那行的格式是 xi1xi2... xin。(将有完全 n 个不同的空格分隔的字符串)。因此,对于 n = 3和变量序为 x3,x1,x2,这一行看起来如下: x3 x1 x2在下一行中,给出了0和1在终端节点上的分布。正好有2 ^ n 个字符(每个字符都可以是0或1) ,后面跟着新行字符。这些字符按照它们在 S 树中出现的顺序给出,第一个字符对应于 S 树的最左端节点,最后一个字符对应于它的最右端节点。下一行包含一个整数 m,即 VVA 的数量,后面是描述它们的 m 行。每个 m 行都包含精确的 n 个字符(每个字符都可以是0或1) ,后跟一个新行字符。不管 S 树的变量顺序如何,第一个字符总是描述 x1的值,第二个字符描述 x2的值,依此类推。因此,行110对应于 VVA (x1 = 1,x2 = 1,x3 = 0)。输入由一个以 n = 0开始的测试用例终止。不应处理此测试用例。
输出
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.
对于每个 S- 树,输出行“ S-Tree # j:”,其中 j 是 S- 树的数目。然后为每个给定的 m VVA 打印一行包含 f (x1,x2,... ,xn)的值,其中 f 是由 S- 树定义的函数。在每个测试用例之后输出一个空行。
样例输入
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
来源
Mid-Central European Regional Contest 1999
分析
可以按照树的遍历去做,这样思路直接。但是可以看到最后遍历树只需要知道走到哪一个叶子节点即可。
二分思路来源在于:走左子树,则最终结果一定不在叶子节点的右半侧,走右子树,最终结果一定不在叶子节点的左半侧,如此设定start,end指定搜索下标的范围,不断缩小范围,同时需要配上手工先行验证想法。
深度为n的树,叶子节点有2 n 2^n2
n
个,用数组char terminal_node[150]存储,只需要找到遍历最后的下标index即可。设叶子节点数组下标从1开始存放数字,且start=1,end=2 n 2^n2
n
,mid=(start+end)/2。对于给定的VVA,遍历前n-1个VVA的值,如果某个变量为0,走左子树,那么最终的值不可能在mid右边,那么修改end=mid,mid=(start+end)/2,如果变量为1同理,不可能在mid左边,修改start=mid,mid=(start+end)/2,按照这个步骤最终遍历完n-1个VVA,如果第n个VVA的值为0,则index = mid即为最终结果在叶子节点的下标,如果VVA=1,则index = mid+1,最后结果为terminal_node[index]
代码
#include <iostream>
#include <string>
#include <cstdio>
using namespace std;
//Accepted
int pow(int x, int y) {
int result = 1;
for (int i = 0; i < y; i++)
result *= x;
return result;
}
int s_tree_function(char vva[], int n) {
int start = 1, end = (int)pow(2, n);
int mid = (start + end) / 2;
for (int i = 0; i < n - 1; i++) {
if (vva[i] == '0') {
end = mid;
mid = (start + mid) / 2;
}
else {
start = end;
mid = (end + mid) / 2;
}
}
if (vva[n - 1] == '1') mid++;
return mid;
}
int main() {
int N;
int cases = 0;
while (cin >> N && N != 0) {
cases++;
string tmp;
for (int i = 0; i < N; i++) {
cin >> tmp;
}
char terminal_node[150];
cin.ignore(1);
for (int i = 1; i <= (int)pow(2, N); i++) {
terminal_node[i] = getchar();
}
int M; cin >> M;
printf("S-Tree #%d:\n", cases);
for (int i = 0; i < M; i++) {
cin.ignore(1);
char vva[10];
for (int i = 0; i < N; i++) {
vva[i] = getchar();
}
int index = s_tree_function(vva, N);
cout << terminal_node[index];
}
cout << endl << endl;
}
return 0;
}