POJ 题目1105 S-Trees(二叉树模拟)

2023-05-16

S-Trees
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 1499 Accepted: 807

Description

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.

Input

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.

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

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  

Source

Mid-Central European Regional Contest 1999


题目大意:1走右孩子,0走左孩子,给出三层节点的顺序,给出叶子节点的值,给出路径,问最后的到达叶子节点的值

ac代码

Problem: 1105		User: kxh1995
Memory: 184K		Time: 0MS
Language: C++		Result: Accepted

#include<stdio.h>
#include<string.h>
#include<math.h>
char a[1010];
char ans[100010];
char str[10][2],op[10];
int main()
{
	int n,c=0;
	while(scanf("%d",&n)!=EOF,n)
	{
		int temp=pow(2.0,n),i;
		for(i=0;i<n;i++)
		{
			scanf("%s",str[i]);
		}
		scanf("%s",a+temp);
		int m,k=0;
		scanf("%d",&m);
		for(i=0;i<m;i++)
		{
			int tmp=1;
			scanf("%s",op);
			for(int j=0;j<n;j++)
			{
				if(op[str[j][1]-'0'-1]=='0')
					tmp*=2;
				else
					tmp=tmp*2+1;
			}
			ans[k++]=a[tmp];
		}
		ans[k]='\0';
		printf("S-Tree #%d:\n",++c);
		printf("%s\n\n",ans);
	}
}


本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

POJ 题目1105 S-Trees(二叉树模拟) 的相关文章

  • POJ-2453

    As we known data stored in the computers is in binary form The problem we discuss now is about the positive integers and
  • POJ 滑动窗口(优先队列的应用)

    数据结构与算法A 第三章 栈与队列 练习题 滑动窗口 思路 对于最大最小值分别维护一个优先队列 xff08 保存元素下标 xff09 以最小值为例 每次遇到一个新元素 xff0c 从队尾插入 插入时删去队列中比该值大的元素 xff08 因为
  • POJ滑动窗口

    题目描述 现在有一堆数字共N个数字 xff08 N lt 61 10 6 xff09 xff0c 以及一个大小为k的窗口 现在这个从左边开始向右滑动 xff0c 每次滑动一个单位 xff0c 求出每次滑动后窗口中的最大值和最小值 例如 xf
  • POJ 3764--The xor-longest Path---DFS + Trie(最大异或值)

    POJ 3764 The xor longest Path Time Limit 2000MS Memory Limit 65536K Description In an edge weighted tree the xor length
  • POJ 2449 Remmarguts‘ Date---SPFA求评估函数 + A*最小堆BFS

    POJ 2449 Remmarguts Date Time Limit 4000MS Memory Limit 65536K Description Good man never makes girls wait or breaks an
  • POJ - 2823 滑动窗口

    题目 xff1a 给一个长度为 NN 的数组 xff0c 一个长为 KK 的滑动窗体从最左端移至最右端 xff0c 你只能看到窗口中的 KK 个数 xff0c 每次窗体向右移动一位 找出窗体在各个位置时的最大值和最小值 思路 xff1a 网
  • POJ 3259 Wormholes(负权环路)

    题意 xff1a 农夫约翰农场里发现了很多虫洞 xff0c 他是个超级冒险迷 xff0c 想利用虫洞回到过去 xff0c 看再回来的时候能不能看到没有离开之前的自己 xff0c 农场里有N块地 xff0c M条路连接着两块地 xff0c W
  • POJ 题目1239 ||ZOJ 题目 1499 Increasing Sequences(正反两次DP)

    Increasing Sequences Time Limit 1000MS Memory Limit 10000KTotal Submissions 3025 Accepted 1147 Description Given a strin
  • 【leetcode】96. 不同的二叉搜索树(unique-binary-search-trees)(DP)[中等]

    链接 https leetcode cn com problems unique binary search trees 耗时 解题 xff1a 51 min 题解 xff1a 36 min 题意 给定一个整数 n xff0c 求以 1 n
  • 【leetcode】95. 不同的二叉搜索树 II(unique-binary-search-trees-ii)(DP)[中等]

    链接 https leetcode cn com problems unique binary search trees ii 耗时 解题 xff1a 0 5 day 题解 xff1a 36 min 题意 给定一个整数 n xff0c 生成
  • poj 2513 colored sticks

    代码 include lt iostream gt include lt stdio h gt using namespace std define MAX 27 define S 500003 struct Node int id Nod
  • TIDB-Error 1105: Out Of Memory Quota问题解决

    一 背景 复杂sql查询报错 二 原因 单条s q l使用内存默认为1G 三 解决 tiup cluster edit config tidb test server configs tidb mem quota query 4294967
  • poj 2096 Collecting Bugs

    Problem poj org problem id 2096 vjudge net contest 151678 problem Q Reference blog csdn net xingyeyongheng article detai
  • poj 1742 Coins

    Problem poj org problem id 1742 Reference www cppblog com flyinghearts archive 2010 09 01 125555 html blog csdn net wang
  • 【题解】poj2689(LibreOJ10197) 线性筛

    题目链接 筛出2到sqrt u 的所有质数 再标记 l u 中是质数p倍数的数 最后枚举相邻质数 部分代码实现参考了大佬题解 题目描述 给定两个整数 L R L R L R 求闭区间 L
  • POJ--1458:Common Subsequence (DP求最长公共子序列)

    1 题目源地址 http poj org problem id 1458 2 基本题意 给出两个序列 求出最长子序列的长度并输出 经典的动态规划求解 求最长公共子序列的经典DP解法代价为O mn 其中m和n分别为两个字符串的长度 具体实现如
  • poj 1330 Nearest Common Ancestors

    Problem poj org problem id 1330 vjudge net contest 80844 problem C Meaning 求最近公共祖先 Note 真 LCA 模版题 那就备份一发 LCA 模版 链式前向星存图
  • poj1240

    本题为已知M 叉树的前序遍历与后序遍历 要求给出对应树有多少种可能 与poj2255类似 只要划分出子树 就把问题规模缩小了 然后就可以递归 目前只会写递归 M叉树的前序遍历为 根 子树1 子树2 子树K K lt M M叉树的后序遍历为
  • 【扩展KMP】POJ_3450| HDU_2328 Corporate Identity

    原题直通车 POJ 3450 Corporate Identity HDU 2328 Corporate Identity 题意概述 找出N个串中最长公共子串 分析 一 可以直接枚举其中一个串的所有字串 跟所有串进行匹配找到结果 二 用其中
  • poj1463

    1

随机推荐

  • windows efi分区修复

    在windows下一阵瞎搞 xff0c 把efi分区的efi标识搞没了 xff0c 导致deepin无法识别efi分区 xff0c update grub2命令失败 其实windows也找不到efi标识了 xff0c 但没有影响启动 因为W
  • XML 根级别上的数据无效。 行 1,位置 1

    上午 xff1a 将XML数据保持到数据中 xff0c 从数据库提取XML 顺利通过 下午 xff1a 一键还原电脑 xff0c 重新打开VS2010运行程序 xff0c 从数据库提取XML报错 根级别上的数据无效 行 1 xff0c 位置
  • 接触客户、接触业务、来谈我的感触

    很久没有做工作总结 xff0c 今天记录下我今年接触客户的一些感触 以前是一个刚入门的开发新人 xff0c 刚进公司感觉公司的开发能力不行 xff0c 没有一套成熟的框架 xff0c 没有美工 xff0c 已经开发出的软件界面很丑 自己开发
  • 编写一个函数,判断用户传入的对象(字符串,列表,元组)长度是否大于10

    span class token keyword def span span class token function my object span span class token punctuation span n span clas
  • 双盘(一个固态硬盘+机械硬盘(efi格式)+2080TI) 安装: ubuntu16.04+显卡驱动+cuda10.2+pytorch

    详细步骤 xff1a 从零开始配一个深度学习服务器 xff1a 固态 43 机械双硬盘ubuntu系统的安装 分区 配置超详细教程 it610 com 一 系统安装 Ubuntu6 04安装 UEFI 43 GPT双硬盘安装Win10 43
  • Idea java.lang.ClassNotFoundException: org.slf4j.LoggerFactory 报错

    系统想用slf4j记录日志 xff0c 可是程序编译的时候报错 xff1a java lang ClassNotFoundException org slf4j LoggerFactory 检查了POM依赖和Jar包 xff0c 都没有问题
  • 小米2020校招软件开发工程师笔试题一

    1 下列关于设计模式说法错误的是 xff08 B xff09 A 装饰器模式在实现过程中一般不会更改被封装对象的接口定义 B 适配器模式以不改变被适配对象的接口定义为目的对其进行改造 C 用饿汉方式实现的单例模式是不能够被继承的 D 简单工
  • vscode1.70.2 添加终端 powershell7

    前提已经安装号powershell7 xff0c 并且已经添加到系统环境中 如何测试是否配置成功呢 xff0c 在运行中输入pwsh xff0c 点击确定后能打开powershell7就说明成功了 打开vscode的设置 xff0c 搜索
  • twm配置文件.twmrc

    系统的twmrc文件位于 usr X11 twm目录下 xff0c 为system twmrc xff0c 但是修改这个文件是不生效的 xff0c 必须将这个文件拷到 HOME下 xff0c 重命名为 twmrc才生效 twm有一个特别奇怪
  • Redis缓存型数据库实现秒杀库存加减

    多线程并发下商品库存递减或者抢购商品数量累加 xff0c 可以使用increment 方法 通常使用异步的方式 xff0c 前端 61 gt 用户抢购处理 61 gt 缓存 61 gt 队列 61 gt 持久化 xff0c 可以使用入队列的
  • Java项目:医院住院管理系统(java+SSM+JSP+bootstrap+mysql)

    源码获取 xff1a 俺的博客首页 34 资源 34 里下载 xff01 项目介绍 本项目有多种角色 xff0c 包含管理员 用户 护士 服务前台等角色 由于篇幅限制 xff0c 只截图了管理员角色的功能 管理员角色主要功能包括 xff1a
  • 直接使用X11输出图像的方法

    直接使用X11输出图像有两种方法 xff0c 一种是使用XImage xff0c 另一种是使用Pixmap XImage是本地对象 xff0c 而Pixmap是XServer端对象 特别要注意的是X11中Bitmap是黑白位图的意思 xff
  • python文件读写的缓冲行为

    文件的io操作的缓冲行为分为 全缓冲 xff1a 同系统及磁盘块大小有关 xff0c n个字节后执行一次写入操作 行缓冲 xff1a 遇到换行符执行一次写操作 无缓冲 xff1a 立刻执行写操作 open 函数 help open Help
  • 在Linux中使用systemctl如何管理Systemd服务和单元

    Systemctl是一个systemd工具 xff0c 它负责控制systemd系统和服务管理程序 Systemd是一个系统管理守护进程 xff0c 工具和库的集合 xff0c 它用作替换System V init守护进程 Systemd功
  • iOS中的viewpager,开源库WMPageController在storyboard中的使用

    http blog csdn net yubo 725 article details 51159633 关于WMPageController的使用以上链接写的很清楚 xff0c 在此感激博主 android 中有viewpager xff
  • 约瑟夫环问题研究(一)

    最近在看C方面的东西 xff0c 看到了李明老师讲解的约瑟夫环问题 xff0c 感觉很有意思 xff0c 于是在看了他的讲解之后 xff0c 自己又重新按照他的思路进行了编写 xff0c 整理如下 xff1a 问题描述 xff1a 约瑟夫环
  • HDOJ 题目2050 折线分割平面(递推)

    折线分割平面 Time Limit 2000 1000 MS Java Others Memory Limit 65536 32768 K Java Others Total Submission s 16441 Accepted Subm
  • HPUOJ 题目1079 假币问题(三分)

    1079 假币问题 时间限制 1 Sec 内存限制 128 MB 提交 7 解决 1 提交 状态 讨论版 题目描述 居然有假币 xff01 xff01 xff01 事情是这样的 xff0c 现在猪肉涨了 xff0c 但是农民的工资却不见涨啊
  • HPUoj 题目1019 【C语言训练】尼科彻斯定理(水题,数学)

    1019 C语言训练 尼科彻斯定理 时间限制 1 Sec 内存限制 128 MB 提交 9 解决 5 提交 状态 讨论版 题目描述 验证尼科彻斯定理 xff0c 即 xff1a 任何一个正整数的立方都可以写成一串连续奇数的和 输入 任一正整
  • POJ 题目1105 S-Trees(二叉树模拟)

    S Trees Time Limit 1000MS Memory Limit 10000KTotal Submissions 1499 Accepted 807 Description A Strange Tree S tree over