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 ...
随机推荐
- Codefoces909E Coprocessor(拓扑排序)
http://codeforces.com/problemset/problem/909/E 由于分了两个queue,所以push的时候可以统一操作,不会影响彼此.两个queue相当于是平等的,只是q ...
- 3D引擎为什么使用三角形绘制曲面
这个问题是我第一次接触3D开发就有的疑问,最近在看<游戏引擎架构>(Game Engine Architecture),在书中找到了答案. 三角网格(Triangle Mesh),游戏开发 ...
- Elastic{ON}参会随手记
Elastic{ON} 同事送了一张Elastic{ON}的票,因为我们的产品中用到的ELK全家桶,实话说用的体量还挺大的,因此非常想去参加这次的发布会. 7.0的新特性 上午的会议是来自总部的一名工 ...
- js实现图片旋转
1.以下代码适用ie9版本 js代码如下: function rotate(o,p){ var img = document.getElementById(o); if(!img || !p) ret ...
- Eclipse (indigo) 中安装jdk包并执行Maven
为安装Eclipsejdk. windows->preferences->java->install jre->add sdk 假设在eclipse里增加M2 Maven 执行 ...
- space.php
天云信息技术有限公司 | 苏ICP备16033617号 1 0.275 ms SELECT * FROM uchome_session WHERE uid='1' Explain id select_ ...
- grid - 网格项目对齐方式(Box Alignment)
CSS的Box Alignment Module补充了网格项目沿着网格行或列轴对齐方式. <view class="grid"> <view class='ite ...
- easylog -- Linux 下的简单日志库
之前使用 log4c 或者 log4cpp 的时候, 总需要配置一些文件和链接库之类复杂的配置. 虽然越复杂越说明这个软件支持的功能多.可选择性强, 但是对于一个小的项目,或者要研究他人的代码而加点儿 ...
- Redis】Java中使用Jedis操作Redis(Maven导入包)、创建Redis连接池
如果我们使用Java操作Redis, 需要确保已经安装了 redis 服务及 Java redis 驱动. Maven项目可以直接在pom.xml中加入jedis包驱动: <!-- https: ...
- 解决CEF中显示Flash动画弹出安全警告问题
一. 1.Xilium.CefGlue. CefApp (CefApp.cs文件)类on_before_command_line_processing方法内设置flash路径.版本号等. m_comm ...