B - Tree Recovery
Little Valentine liked playing with binary trees very much. Her favorite game was constructing randomly looking binary trees with capital letters in the nodes.
This is an example of one of her creations:
D
/ \
/ \
B E
/ \ \
/ \ \
A C G
/
/
F
To record her trees for future generations, she wrote down two strings for each tree: a preorder traversal (root, left subtree, right subtree) and an inorder traversal (left subtree, root, right subtree). For the tree drawn above the preorder traversal is DBACEGF and the inorder traversal is ABCDEFG.
She thought that such a pair of strings would give enough information to reconstruct the tree later (but she never tried it).
Now, years later, looking again at the strings, she realized that reconstructing the trees was indeed possible, but only because she never had used the same letter twice in the same tree.
However, doing the reconstruction by hand, soon turned out to be tedious.
So now she asks you to write a program that does the job for her!
Input
The input will contain one or more test cases.
Each test case consists of one line containing two strings preord and inord, representing the preorder traversal and inorder traversal of a binary tree. Both strings consist of unique capital letters. (Thus they are not longer than 26 characters.)
Input is terminated by end of file.
Output
For each test case, recover Valentine's binary tree and print one line containing the tree's postorder traversal (left subtree, right subtree, root).
Sample Input
DBACEGF ABCDEFG
BCAD CBAD
Sample Output
ACBFGED
CDAB
题意:给你前序和中序,求后序;只要找到规律就可以,前序的第一个一定是子树的根节点,在中序的根节点左边的是根节点的左子树,一直找下去,直到不能找了输出
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include <iomanip>
#include<cmath>
#include<float.h>
#include<string.h>
#include<algorithm>
#define sf scanf
#define pf printf
#define mm(x,b) memset((x),(b),sizeof(x))
#include<vector>
#include<queue>
#include<map>
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=a;i>=n;i--)
typedef long long ll;
const ll mod=1e9+100;
const double eps=1e-8;
using namespace std;
const double pi=acos(-1.0);
const int inf=0xfffffff;
const int N=1005;
char a[30],b[30];
void find(int l1,int r1,int l2,int r2)
{
if(l1>r1) return ;
int y=l2;
while(b[y]!=a[l1]) y++;
find(l1+1,r1-(r2-y),l2,y-1);
find(r1+1-(r2-y),r1,y+1,r2);
pf("%c",a[l1]);
}
int main()
{
while(~sf("%s%s",a,b))
{
int l=strlen(a);
find(0,l-1,0,l-1);
pf("\n");
}
return 0;
}
B - Tree Recovery的更多相关文章
- POJ 2255. Tree Recovery
Tree Recovery Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11939 Accepted: 7493 De ...
- Tree Recovery(前序中序求后序)
Tree Recovery Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14640 Accepted: 9091 De ...
- poj 2255 Tree Recovery 分治
Tree Recovery Description Little Valentine liked playing with binary trees very much. Her favorite g ...
- Tree Recovery POJ - 2255
Tree Recovery POJ - 2255 根据树的前序遍历和中序遍历还原后序遍历. (偷懒用了stl的find) #include<iostream> #include<st ...
- 2018年全国多校算法寒假训练营练习比赛(第五场)H Tree Recovery
Tree Recovery 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 131072K,其他语言262144K 64bit IO Format: %lld 链接:https://w ...
- POJ 2255 Tree Recovery 二叉树的遍历
前序和中序输入二叉树,后序输出二叉树:核心思想只有一个,前序的每个根都把中序分成了两部分,例如 DBACEGF ABCDEFG D把中序遍历的结果分成了ABC和EFG两部分,实际上,这就是D这个根的左 ...
- Tree Recovery(由先、中序列构建二叉树)
题目来源: http://poj.org/problem?id=2255 题目描述: Description Little Valentine liked playing with binary tr ...
- poj 2255 Tree Recovery(求后序遍历,二叉树)
版权声明:本文为博主原创文章,未经博主同意不得转载.vasttian https://blog.csdn.net/u012860063/article/details/37699219 转载请注明出处 ...
- UVa 536 Tree Recovery(二叉树后序遍历)
Little Valentine liked playing with binary trees very much. Her favorite game was constructing rando ...
随机推荐
- python测试开发django-51.Ajax发送post请求登录案例
前言 我想实现一个登录功能:登录的接口是另外一个地方提供,页面上点登录按钮的时候,先访问登录接口,根据接口返回json信息判断是否登录成功,登录成功页面跳转,登录不成功,在登录首页显示失败原因 登录页 ...
- Java全栈程序员之08:MAVEN+JAVA配置
从Spring3.0开始,Spring支持以Java配置的方式来代替XML配置.这一点说起来其实有点可笑,XML配置的方式最初被创建出来就是为了让配置与程序员无关.可是最终我们发现,绝大多数的那些配置 ...
- Css3实现波浪线效果1
一.波浪线 ,常用 .info::before { content: ''; position: absolute; top: 30px; width: 100%; height: 0.25em; b ...
- C++异常处理解析: 异常的引发(throw), 捕获(try catch)、异常安全
前言: C++的异常处理机制是用于将运行时错误检测和错误处理功能分离的一 种机制(符合高内聚低耦合的软件工程设计要求), 这里主要总结一下C++异常处理的基础知识, 包括基本的如何引发异常(使用th ...
- Redis简单示例
1.Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API. 从2010年3月15日起,Redis的开发工作由VMwa ...
- gmock
https://www.cnblogs.com/welkinwalker/archive/2011/11/29/2267225.html http://www.cnblogs.com/jycboy/p ...
- 【nginx&php】后台权限认证方式
一.最常用的方法(代码中限制) 1.如何限制IP function get_new_ip(){ if(getenv('HTTP_CLIENT_IP')) { $onlineip = getenv('H ...
- dos命令大全 黑客必知的DOS命令集合
dos命令大全 黑客必知的DOS命令集合 一般来说dos命令都是在dos程序中进行的,如果电脑中安装有dos程序可以从开机选项中选择进入,在windows 系统中我们还可以从开始运行中输入cmd命令进 ...
- 分析轮子(十)- HashMap.java 之概念梳理
注:玩的是JDK1.7版本 一:还是原来的风格,先上一下类的继承关系图,这样能够比较清楚的知道此类的相关特性 二:HashMap.java 的代码比较难看,所以,我看了几天,写的话也分开来写,这样能表 ...
- springboot配置idea 热部署
背景: 在开发中,当我们修改代码之后,每次都要重新启动,很是浪费时间,在springboot中就有一种热部署方式,可以实现想要修改不需要每次都重新启动,保存即可生效 用法: 一.maven 添加 ...