UVA 712(二叉树模拟)

L - S-Trees

Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld
& %llu

Submit Status

Appoint description: 
System Crawler  (2014-04-01)

Description

 S-Trees 

A Strange Tree (S-tree) over the variable set  is a binary
tree representing a Boolean function . 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  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 ,
then it is quite simple to find out what  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 function 

On the picture, two S-trees representing the same Boolean function, ,
are shown. For the left tree, the variable ordering is x1x2x3, and for the right tree it is x3x1x2.

The values of the variables , are given as a Variable Values Assignment (VVA)

with . 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 . 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  as
described above.

Input

The input file 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,
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 xi1xi2 ... xin. (There will be exactly n different
space-separated strings). So, for n = 3 and the variable ordering x3x1x2, 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 2n 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 exactlyn 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

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  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
000
010
111
110
3
x3 x1 x2
00010011
4
000
010
111
110
0

Sample Output

S-Tree #1:
0011

S-Tree #2:
0011

Miguel A. Revilla

2000-02-09

就是给出n个变量,变量为1往右走,为0往左走,问到达最底层时的值是多少,数组模拟即可。

#include<stdio.h>
#include<string.h>
char ans[1005],temp[2005];
int n,o[1<<9],num;
char s[1<<9],b[1<<9];
int main()
{
    int cas=1;
    //freopen("in.txt","r",stdin);
    while(~scanf("%d",&n)&&n)
    {
        getchar();
        gets(temp);
        int len=strlen(temp);
        num=0;
        int k=1,ok=0;
        for(int i=0;i<=len;i++)
        {
            if((temp[i]>='0'&&temp[i]<='9'))
            {
                num+=num*10+temp[i]-'0';
                ok=1;
            }
            else if(ok)
            {
                o[k++]=num;
                //printf("%d\n",num);
                ok=num=0;
            }
        }
        int m;
        scanf("%s%d",s+1,&m);
        int t=0;
        while(m--)
        {
            int k=1;
            scanf("%s",b+1);
            for(int i=1;i<=n;i++)
            {
                if(b[o[i]]-'0')k=2*k+1;
                else k=2*k;
            }
            k-=(1<<n)-1;
            ans[t++]=s[k];
        }
        ans[t]='\0';
        printf("S-Tree #%d:\n%s\n\n",cas++,ans);
    }
    return 0;
}

UVA 712(二叉树模拟),布布扣,bubuko.com

时间: 2024-10-01 11:46:04

UVA 712(二叉树模拟)的相关文章

S-Tree (UVa 712) 二叉树

题目:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=839&page=show_problem&problem=653 思路:当前结点为k,左走则 2k,右走则 2k + 1,得到最后的点,减去前面n层的所有结点,得到的下标对应的0或1就是答案. /* S-Tree (UVa 712) */ #include <iostream> #include &

二叉树模拟

接着我们就要写一个比较复杂的数据结构的,但是这个数据结构是很重要的,假如你想深入的学习算法等等.我们来模拟一下二叉树. public class BiTree { private BiTree leftTree;// 左子树 private BiTree rightTree;// 右子树 private Object data;// 节点数据 public final static int MAX = 40; BiTree[] elements = new BiTree[MAX];// 层次遍历

UVa 712 - S-Trees

题目:给你一棵全然二叉树,再给你一个xi1~xin-1的序列,相应从根到叶子上一层. 每层相应一个xi,叶子是0或1,如今给你一些路径,输出路径相应的叶子的值: 路径是01串.从更開始往下走,0表示左子树,1表示右子树,相应x1~xn-1的顺序. 分析:模拟. 直接安找路径走过去就好了,由于是二叉树,正好相应二进制数. 注意:路径的顺序要调整到和树一样在计算. #include <algorithm> #include <iostream> #include <cstdlib

UVA 246 - 10-20-30 (模拟+STL)

UVA 246 - 10-20-30 题目链接 题意:给52张的扑克堆,先从左往右发7张牌,之后连续不断从左往右发7张牌,如果有牌堆形成了以下3种情况(按顺序判断): 1.头两张+尾一张和为10或20或30 2.头一张+尾两张和为10或20或30 3.尾三张和为10或20或30 就把这三张牌拿走,放到总牌堆底(这步要不断执行直到不再满足条件或牌堆没了) 如果有一个牌堆因为这个操作被取完了,那么以后将不在这个位置发牌. 如果最后7个牌堆都可以消掉,那么赢,总牌堆用完,那么输,否则平(即不断循环)

uva 11234(二叉树、线性表)

题解:可以根据输入的字符串画一个二叉树出来,然后层次遍历一下就和输出结果顺序一样,所以根据输入把大写字母当做两个小写字母的根节点,将节点即运算结果当做另一个小写字母放进栈里,和另一个字母组建生成新的树放进栈里,直到最后的的根节点也放进了栈里,开始层次遍历,用数组模拟队列进行遍历,注意因为结果的顺序是从右到左,所以注意遍历的方向问题. #include <cstdio> #include <stack> #include <queue> #include <cstd

UVa 548 (二叉树的递归遍历) Tree

题意: 给出一棵由中序遍历和后序遍历确定的点带权的二叉树.然后找出一个根节点到叶子节点权值之和最小(如果相等选叶子节点权值最小的),输出最佳方案的叶子节点的权值. 二叉树有三种递归的遍历方式: 先序遍历,先父节点  然后左孩子  最后右孩子 中序遍历,先左孩子  然后父节点  最后父节点 后序遍历,先左孩子  然后右孩子  最后父节点 这里有更详细的解释: http://blog.csdn.net/sicofield/article/details/9066987 紫书上面写错了,后序遍历最后一

UVa 699 (二叉树) The Falling Leaves

题意: 按先序方式输入一棵二叉树,节点是带权的,左孩子在父节点的左一个单位,右孩子在父节点的右一个单位,从左到右输出相同水平位置节点之和. 分析: 做了好几道二叉树的题,代码应该也很好理解了.这里maxn开始设了200.500都RE,后来索性开了2000,AC了 紫书上面init函数最后应该加一句 return true; 1 //#define LOCAL 2 #include <iostream> 3 #include <cstdio> 4 #include <cstri

UVa 11988 (数组模拟链表) Broken Keyboard (a.k.a. Beiju Text)

题意: 模拟一个文本编辑器,可以输入字母数字下划线,如果遇到'['则认为是Home键,如果是']'则认作End键. 问最终屏幕上显示的结果是什么字符串. 分析: 如果在数组用大量的移动字符必然很耗时.所以next数组表示显示屏中s[i]右边的字符编号,变量cur模拟光标,即当前光标位于s[cur]的右边. 变量last记录显示屏最后一个字符的下标. 我理解的连接的情况应该是这样子的: 1 //#define LOCAL 2 #include <cstdio> 3 #include <cs

uva 548(二叉树)

题解:给出了二叉树的中序和后序.重建二叉树.输出路径和最短的叶子的值. 两个模板: 给出前序和中序建树: Node* build (int n, int* preo, int* ino) { Node* node = new Node; int i = 0; if (n <= 0) return NULL; while (ino[i] != preo[0]) i++; node -> val = ino[i]; node -> left = build(i, preo + 1, ino)