【UVa 712】S-Trees

S-Trees

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

Submit Status

Description

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 xi1corresponding 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 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

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

题目看了半天。。。

搞懂了,其实很简单,用叶子节点构建一棵完全二叉树,然后按查询查找即可。

#include<cstdio>
#include<cstring>

using namespace std;

int n, m, kase;
char s[1 << 8], cmd[1 << 8];

int main()
{
    kase = 0;
    while (scanf("%d", &n) == 1 && n)
    {
        for (int i = 0; i < n; ++i) scanf("%*s");
        scanf("%s", s);
        scanf("%d", &m);
        printf("S-Tree #%d:\n", ++kase);
        for (int i = 0; i < m; ++i)
        {
            scanf("%s", cmd);
            int u = 0;
            for (int j = 0; j < n; ++j)
                if (cmd[j] == ‘1‘) u = 2 * u + 1;
                else u *= 2;
            printf("%c", s[u]);
        }
        printf("\n\n");
    }
    return 0;
}
时间: 2024-11-13 16:16:53

【UVa 712】S-Trees的相关文章

【uva 658】It&#39;s not a Bug, it&#39;s a Feature!(图论--Dijkstra算法+二进制表示)

题意:有n个潜在的bug和m个补丁,每个补丁用长为n的字符串表示.首先输入bug数目以及补丁数目.然后就是对m 个补丁的描述,共有m行.每行首先是一个整数,表明打该补丁所需要的时间.然后是两个字符串,地一个字符串 是对软件的描述,只有软件处于该状态下才能打该补丁该字符串的每一个位置代表bug状态(-代表该位置没bug,+代 表该位置有bug,0表示该位置无论有没有bug都可打补丁).然后第二个字符串是对打上补丁后软件状态的描述 -代表该位置上的bug已经被修复,+表示该位置又引入了一个新的bug

【UVa 10881】Piotr&#39;s Ants

Piotr's Ants Porsition:Uva 10881 白书P9 中文改编题:[T^T][FJUT]第二届新生赛真S题地震了 "One thing is for certain: there is no stopping them;the ants will soon be here. And I, for one, welcome our new insect overlords."Kent Brockman Piotr likes playing with ants. H

【UVa 10815】Andy&#39;s First Dictionary

真的只用set和string就行了. 如果使用PASCAL的同学可能就要写个treap什么的了,还要用ansistring. #include<cstdio> #include<cstring> #include<iostream> #include<string> #include<set> using namespace std; string s; string now; set<string> dict; bool is_al

【UVA - 10815】Andy&#39;s First Dictionary (set)

Andy's First Dictionary Description 不提英文了 直接上中文大意吧 XY学长刚刚立下了再不过CET就直播xx的flag,为了不真的开启直播模式,XY学长决定好好学习英语.于是他每天都读一篇只包含生词的英语文章,并以自己高达450的智商在一秒钟之内记忆下来. 现在给你一篇XY学长今天要读的文章,请你写一个程序,输出他都学习到了哪些单词.要求:如果文章中有相同的单词,那么仅仅输出一次:而且如果两个单词只有大小写不同,将他们视为相同的单词. Input 测试数据将输入

【UVA 1395】 Slim Span (苗条树)

[题意] 求一颗生成树,满足最大边和最小边之差最小 InputThe input consists of multiple datasets, followed by a line containing two zeros separated by a space.Each dataset has the following format.n ma1 b1 w1...am bm wmEvery input item in a dataset is a non-negative integer.

【UVA 1151】 Buy or Build (有某些特别的东东的最小生成树)

[题意] 平面上有n个点(1<=N<=1000),你的任务是让所有n个点连通,为此,你可以新建一些边,费用等于两个端点的欧几里得距离的平方. 另外还有q(0<=q<=8)个套餐,可以购买,如果你购买了第i个套餐,该套餐中的所有结点将变得相互连通,第i个套餐的花费为ci. 求最小花费. Input (1 ≤ n ≤ 1000)  (0 ≤ q ≤ 8). The second integer is the the cost of the subnetwork(not greater

【UVA 10369】 Arctic Network (最小生成树)

[题意] 南极有n个科研站, 要把这些站用卫星或者无线电连接起来,使得任意两个都能直接或者间接相连.任意两个都有安装卫星设备的,都可以直接通过卫星通信,不管它们距离有多远. 而安装有无线电设备的两个站,距离不能超过D. D越长费用越多. 现在有s个卫星设备可以安装,还有足够多的无线电设备,求一个方案,使得费用D最少(D取决与所有用无线电通信的花费最大的那条路径). InputThe first line of input contains N, the number of test cases.

【uva 10954】Add All(算法效率+Huffman编码+优先队列)

题意:有N个数,每次选2个数合并为1个数,操作的开销就是这个新的数.直到只剩下1个数,问最小总开销. 解法:合并的操作可以转化为二叉树上的操作[建模],每次选两棵根树合并成一棵新树,新树的根权值等于两棵合并前树的根权值和(也与Huffman编码的建立过程类似,选权值最小的两棵树). 这样总开销就是除了叶子结点的权值和  => 每个叶子结点的权值*层数(根节点层数为0)之和  => WPL(树的所有叶子节点的带权路径长度之和,即该节点到根节点路径长度与节点上权的乘积之和). 而Huffman树就

【uva 10570】Meeting with Aliens(算法效率--暴力+贪心)

题意:输入1~N的一个排列,每次可以交换2个整数,问使排列变成1~N的一个环状排列所需的虽少交换次数.(3≤N≤500) 解法:(又是一道我没打代码,光想和看就花了很久时间的题~QwQ)由于n很小,可以暴力枚举目标的环状排列,于是贪心交换——把元素 x 直接与它的目标位置上的元素互换,这样至少使1个元素的位置正确了.而若 x 先与其他 k 个元素交换,是最多能得到 k+1 个元素的正确排列的,这样并没有之前的策略优.    另外,网上关于此题还有一种关于对链状序列找环的说法,我更加不理解.若有人