UVa 712 S-Trees(满二叉树数组保存与编号)
来源:互联网 发布:网络通信代维招聘 编辑:程序博客网 时间:2024/05/29 16:57
一.满二叉树
我们发现满二叉树的编号具有这样的性质即根节点为n的话,那么其左子树的编号应该是2n,右子树的编号应该是2n+1
,有了的这样的性质,我们就可以很假单的用线性表来保存满二叉树的节点,注意,这样的性质非满二叉树并不具有,所以不可以用这样的方法来保存。
二.
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.
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)
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.
Input
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.
Output
Output a blank line after each test case.
Sample Input
3x1 x2 x30000011140000101111103x3 x1 x20001001140000101111100
Sample Output
S-Tree #1:0011S-Tree #2:0011
三.分析与实现
解决这种结构化的模拟类问题的时候,一定要明确的就是我们每一步解决的是什么问题,如何解决,解决的步骤是什么,只要一步步盯着眼前的目标解决就好,首先,我们要解决的问题就是根据输入的每一步的决策序列(xi)来找到整个序列所对应的值({0,1}),我们只需要按照给定的顺序遍历序列即可,注意,本问题没有必要储存除了叶子节点之外的其他节点,所以我们采用了,map来使得编号(key)和值对应起来。
#include <iostream>#include <cstring>#include <map>#include <cmath>using namespace std;int n,order[10],m;map <int,int> base;void init1(){string temp;memset(order,0,sizeof(order));for(int i=1;i<=n;i++){cin>>temp;order[i]=temp[1]-'0';}}void init2(){base.clear();int s=pow(2,n);string temp;cin>>temp;//cout<<temp;for(int i=0;i<temp.length();i++){base[s+i]=temp[i]-'0';}//for(int i=0;i<temp.length();i++)//{//cout<<"("<<s+i<<")"<<base[s+i]<<" ";//}}int find(){int num=1;string temp;cin>>temp;for(int i=1;i<=n;i++){int t;t=order[i];if(temp[t-1]=='0') num=num*2;else num=num*2+1;}return base [num];}void test1(){for(int i=1;i<=n;i++){cout<<order[i]<<" ";}cout<<endl;}int main(){//freopen("input.txt","r",stdin);int t=0;while(cin>>n){if(n==0) break;t++;init1();//test1();init2();cin>>m;cout<<"S-Tree #"<<t<<":"<<endl;for(int i=1;i<=m;i++){cout<<find();}cout<<endl;cout<<endl;}return 0;}
- UVa 712 S-Trees(满二叉树数组保存与编号)
- Uva 712 - S-Trees//二叉树
- UVa 712/POJ 1105/ZOJ 1150 S-Trees(用数组模拟二叉树)
- uva 712 S-Trees(利用二叉树的特点)
- UVa 712 S-Trees S树
- S树(S-Trees, Uva 712)
- [UVa 712] S 树(S-Trees)
- UVa 712 S-Trees
- UVa 712 - S-Trees
- Uva 712 S-Trees
- uva 712 - S-Trees
- uva 712 - S-Trees
- UVa 712 - S-Trees
- UVA 712 S-Trees
- uva-712 S-Trees
- uva 712-S-Trees
- UVA 712 S-Trees
- uva 712 S-Trees
- Android程序优化之对屏幕旋转的处理总结
- OneThink修改后台文档列表搜索规则
- java 前端及后台轮询方法总结
- poj 1703 Find them, Catch them
- 【C语言】动态内存分配alloca
- UVa 712 S-Trees(满二叉树数组保存与编号)
- g4l 备份还原linux系统
- cug1699数组反转
- PAT乙级1006:换个格式输出整数 (15)
- Stanford 机器学习 Dimensionality Reduction
- java性能优化之二 循环里面不使用hibernate创建对象
- solr占用CPU持续过高原因查询
- lintcode ----反转整数
- 20160511 GreenPlum9 索引