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 ...
随机推荐
- List集合序列排序的两种方法
首先讲一下Comparable接口和Comparator接口,以及他们之间的差异.有助于Collections.sort()方法的使用.请参考 1.Comparable自然规则排序//在自定义类Stu ...
- WPF模拟键盘输入和删除
private void ButtonNumber_Click(object sender, RoutedEventArgs e) { Button btn = (Button)sender; str ...
- DockerSwarm获取Token与常用命令
一.Token相关 Join tokens是允许一个节点加入集群的密钥.有两种可用的不同的join tokens,一个是用作worker角色,另一个是用作manager角色.在执行swarm join ...
- C# System.IO.FileMode
字段 Append 6 若存在文件,则打开该文件并查找到文件尾,或者创建一个新文件. 这需要 Append 权限. FileMode.Append 只能与 FileAccess.Write 一起使用. ...
- Navicat http 通道增加验证
ntunnel_mysql.php 中增加 function check() { if (!isset($_SERVER['PHP_AUTH_USER'])) { header('WWW-Authen ...
- Visual Studio 2015 update 3各版本下载地址
微软在06月27日发布了Visual Studio 2015 Update 3 .在MSDN中微软也提供下载,而且MSDN的Visual Studio 2015 Update 3与官方免费下载的文件是 ...
- 单片机成长之路(51基础篇) - 022 N76e003 APROM模拟EEPROM驱动
N76e003单片机内部没有EEPROM,但是可以使用 APROM模拟EEPROM功能,代码如下: eeprom.h #ifndef _EEPROM_H_ #define _EEPROM_H_ //E ...
- C# Cache缓存读取设置
先创建一个CacheHelper.cs类,代码如下: using System; using System.Web; using System.Collections; using System.We ...
- 利用cwRsync客户端将Windows下文件同步到Linux
这里不描述Linux服务端安装配置rsync服务的过程,有需要可以在网络上查找相关教程. 1.安装cwRsync客户端下载地址:http://itefix.no/cwrsync/下载文件cwRsync ...
- 【翻译】Nginx的HTTP负载均衡
本文为翻译文,原文地址:http://nginx.org/en/docs/http/load_balancing.html 介绍 将请求负载均衡到多个应用实例是一个常用的技术,它起到优化资源使用率.最 ...