HDU 3791 二叉搜索树 题解
接下去一行是一个序列,序列长度小于10,包括(0~9)的数字,没有反复数字,依据这个序列能够构造出一颗二叉搜索树。
接下去的n行有n个序列,每一个序列格式跟第一个序列一样,请推断这两个序列能否组成同一颗二叉搜索树。
2
567432
543267
576342
0
YES
NO
一条浙大考研上机题目。考二叉树构建。
直接使用静态数组过了,由于数据不大。
#include <stdio.h>
#include <iostream>
#include <string>
using std::string;
using std::cin; const int SIZE = (1<<9)+1;
char Tree[SIZE];
char secTree[SIZE];
string root, child; void constrctTree(char T[], char a, int node = 0)
{
if (T[node] == 'b')
{
T[node] = a;
return ;
}
if (a < T[node]) constrctTree(T, a, node<<1|1);
else constrctTree(T, a, (node+1)<<1);
} int main()
{
int n;
secTree[1<<9] = '\0';
Tree[1<<9] = '\0';
while (scanf("%d", &n) && n != 0)
{
for (int i = 0; i < 1<<9; i++) Tree[i] = 'b';
cin>>root;
for (int i = 0; i < (int)root.size(); i++)
{
constrctTree(Tree, root[i]);
}
for (int i = 0; i < n; i++)
{
cin>>child;
if (root.size() != child.size()) puts("NO");
else
{
for (int j = 0; j < 1<<9; j++) secTree[j] = 'b';
for (int i = 0; i < (int)child.size(); i++)
{
constrctTree(secTree, child[i]);
}
if (strcmp(Tree, secTree) == 0) puts("YES");
else puts("NO");
}
}
}
return 0;
}
HDU 3791 二叉搜索树 题解的更多相关文章
- HDU 3791 二叉搜索树 (数据结构与算法实验题 10.2 小明) BST
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=3791 中文题不说题意. 建立完二叉搜索树后进行前序遍历或者后序遍历判断是否一样就可以了. 跟这次的作业第 ...
- HDU 3791 二叉搜索树
二叉搜索树 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- hdu 3791:二叉搜索树(数据结构,二叉搜索树 BST)
二叉搜索树 Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Submiss ...
- hdu 3791 二叉搜索树(数据结构)
二叉搜索树 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- HDU 3791二叉搜索树解题(解题报告)
1.题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=3791 2.参考解题 http://blog.csdn.net/u013447865/articl ...
- HDU 3179 二叉搜索树(树的建立)
二叉搜索树 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- PAT L3-010 是否完全二叉搜索树(二叉搜索树)
将一系列给定数字顺序插入一个初始为空的二叉搜索树(定义为左子树键值大,右子树键值小),你需要判断最后的树是否一棵完全二叉树,并且给出其层序遍历的结果. 输入格式: 输入第一行给出一个不超过20的正整数 ...
- HDU - 3791 建立二叉搜索树
题意: 给定一个序列,下面又有n个序列,判断这个序列和其他序列是否为同一个二叉树(同一序列数字各不相同) 思路: 首先讲将一个序列建立成二叉搜索树,然后将其他序列也建立二叉搜索树,两个树进行前序遍历, ...
- hdu 3999 The order of a Tree (二叉搜索树)
/****************************************************************** 题目: The order of a Tree(hdu 3999 ...
随机推荐
- Hadoop配置项整理(mapred-site.xml)【转】
本文转自:http://slaytanic.blog.51cto.com/2057708/1101360 name value Description hadoop.job.history.locat ...
- Ajax数据格式,html,xml,json
1. 2. 3. 4. 5. 6. 7. 8. 9.
- oracle特殊字符的ascii值
- Swift - 08 - 元组
//: Playground - noun: a place where people can play import UIKit // 元组就是将多个不同的值集合成一个数据 /* 元组是Object ...
- 重新开始学习javase_集合_List
一,List之ArrayList(转:http://blog.csdn.net/zheng0518/article/details/42198205) 1. ArrayList概述: ArrayLis ...
- WebService cxf 接口中获得拦截器参数
1. 拦截器中put属性 Message message = PhaseInterceptorChain.getCurrentMessage(); message.put("AuthCode ...
- Nginx源码研究七:nginx的location指令分析
在nginx的配置文件nginx.conf中,我们在配置server的时候,会配置一下location指令,这个location指令是提供给用户来配置对于符合指令的http请求,采用该指令内部的处理方 ...
- PHP mysql 删除表中所有数据只保留一条
DELETE FROM `logs` WHERE wangzhi='www.juhutang.com' and id<>101072; 上面这段代码的意思为 删除表logs中 所有字段wa ...
- C语言的算法--------二分法查找
int find(int n,int a[],int l){int low=0;int high=l-1;int middle=0;while(low<high){middle=(low+hig ...
- angular-utils-pagination 使用案例
angular-utils-pagination是基于angular,bootstrap,jquery的一个分页插件,详细介绍以及使用方法参照: Git:https://github.com/mich ...