Problem Description
推断两序列是否为同一二叉搜索树序列
 
Input
開始一个数n,(1<=n<=20) 表示有n个须要推断,n= 0 的时候输入结束。

接下去一行是一个序列,序列长度小于10,包括(0~9)的数字,没有反复数字,依据这个序列能够构造出一颗二叉搜索树。

接下去的n行有n个序列,每一个序列格式跟第一个序列一样,请推断这两个序列能否组成同一颗二叉搜索树。
 
Output
假设序列同样则输出YES,否则输出NO
 
Sample Input
2
567432
543267
576342
0
 
Sample Output
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;
}

最新文章

  1. 03.LoT.UI 前后台通用框架分解系列之——多样的表格
  2. 在eclipse安装svn插件
  3. tornado 学习笔记8 模板以及UI
  4. 从H264码流中获取视频宽高 (SPS帧)
  5. 题目1373:整数中1出现的次数(从1到n整数中1出现的次数)
  6. css 实现三角形的原理
  7. A replacement solution to using Google Drive in Ubuntu
  8. 130 个你需要了解的 vim 命令
  9. ztree树 叶子节点路径的集合
  10. 解决LinearLayout中控件不能居右对齐
  11. CSS 隐藏多余的字符
  12. 《Microsoft SQL Server企业级平台管理实践》笔记
  13. C标准I/O建立一个文件仓库
  14. [C#] 分布式ID自增算法 Snowflake
  15. bzoj:3085: 反质数加强版SAPGAP
  16. codechef Dynamic GCD [树链剖分 gcd]
  17. 沉淀,再出发——手把手教你使用VirtualBox搭建含有三个虚拟节点的Hadoop集群
  18. 第8章 CentOS包管理详解
  19. 举例分析 Makefile 中的 patsubst、wildcard、notdir 函数
  20. NOIp2018 游记

热门文章

  1. c - 递归年龄
  2. .NET Framework 中的类型系统的两个基本点
  3. 安装PHP过程中,make步骤报错:(集合网络上各种解决方法)
  4. PLSQL远程连接到Oracle服务器
  5. jQuery图片滑动
  6. 『重构--改善既有代码的设计』读书笔记----Replace Method with Method Object
  7. Python自动化运维之20、HTML
  8. CodeForces 546C(队列)
  9. PHP之路——MySql查询语句
  10. iOS证书快要过期怎么办?