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的更多相关文章

  1. POJ 2255. Tree Recovery

    Tree Recovery Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11939   Accepted: 7493 De ...

  2. Tree Recovery(前序中序求后序)

    Tree Recovery Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14640   Accepted: 9091 De ...

  3. poj 2255 Tree Recovery 分治

    Tree Recovery Description Little Valentine liked playing with binary trees very much. Her favorite g ...

  4. Tree Recovery POJ - 2255

    Tree Recovery POJ - 2255 根据树的前序遍历和中序遍历还原后序遍历. (偷懒用了stl的find) #include<iostream> #include<st ...

  5. 2018年全国多校算法寒假训练营练习比赛(第五场)H Tree Recovery

    Tree Recovery 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 131072K,其他语言262144K 64bit IO Format: %lld 链接:https://w ...

  6. POJ 2255 Tree Recovery 二叉树的遍历

    前序和中序输入二叉树,后序输出二叉树:核心思想只有一个,前序的每个根都把中序分成了两部分,例如 DBACEGF ABCDEFG D把中序遍历的结果分成了ABC和EFG两部分,实际上,这就是D这个根的左 ...

  7. Tree Recovery(由先、中序列构建二叉树)

    题目来源: http://poj.org/problem?id=2255 题目描述: Description Little Valentine liked playing with binary tr ...

  8. poj 2255 Tree Recovery(求后序遍历,二叉树)

    版权声明:本文为博主原创文章,未经博主同意不得转载.vasttian https://blog.csdn.net/u012860063/article/details/37699219 转载请注明出处 ...

  9. UVa 536 Tree Recovery(二叉树后序遍历)

    Little Valentine liked playing with binary trees very much. Her favorite game was constructing rando ...

随机推荐

  1. HTML5 学习02——新元素:canvas

    HTML5 Canvas <canvas>标签:使用脚本 (通常是JavaScript)来绘制图形——默认情况下 <canvas> 元素没有边框和内容. 在画布上(Canvas ...

  2. Kworkerd恶意挖矿分析

    转自:https://www.360zhijia.com/anquan/417114.html 0x01 快速特征排查 TOP显示CPU占用高,但是没有高占用的进程 存在与未知服务器13531端口建立 ...

  3. 解析 ViewTreeObserver 源码(下)

    继上篇内容,本文介绍 ViewTreeObserver 的使用,以及体会其所涉及的观察者模式,期间会附带回顾一些基础知识.最后,我们简单聊一下 Android 的消息传递,附高清示意图,轻松捋清整个传 ...

  4. vue-cli配置多入口多出口,实现一个项目两个访问地址,区分不同上线环境

    最近工作中需要把项目分割成两块,一块需要跑在微信中,通过微信jdk获取用户资料默认登录,一部分需要给原生app做webview的内嵌页面,当然这部分内容是不跑在微信中的. 所以我想到了把项目分成两部分 ...

  5. Android support 26.0.0-alpha1 产生的问题(zz)

    针对以下两个错误 Java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/v4/animation/Animato ...

  6. git中提示 please tell me who you are

    提示也就是需要你登录一下,确认你的身份,但是不要按照其提示输入,先输入命令git config user.name “username”,换行输入git config user.email “emai ...

  7. shell编程学习笔记(八):Shell中if判断的使用

    一.if的语法: 1.单分支语句结构 if [ 条件表达式 ]; then 指令 fi 2.双分支语句结构 if [ 条件表达式 ]; then 指令一 else 指令二 fi 3.多分支语句结构 i ...

  8. grid - 网格项目层级

    网格项目可以具有层级和堆栈,必要时可能通过z-index属性来指定. 1.在这个例子中,item1和item2的开始行都是1,item1列的开始是1,item2列的开始是2,并且它们都跨越两列.两个网 ...

  9. Linux下终端录制工具-asciinema

    1. 官网 https://asciinema.org/ 2. 功能 录音 录制终端命令 上传到多种位置 3. 使用方法 sudo yum install asciinema # 安装 asciine ...

  10. Implementation of Message Receiver