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:

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 file 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

HINT

Accepted

#include<iostream>
#include<string>
using namespace std; string pre, in; void build(int l1,int r1,int l2,int r2) {
int i = in.find(pre[l1]);
if (i > l2)build(l1 + 1, l1 + i - l2, l2, i - 1);
if (i < r2)build(r1 - r2 + i + 1, r1, i + 1, r2);
cout << pre[l1];
} int main() {
while (cin >> pre >> in) {
build(0, pre.size() - 1, 0, in.size() - 1);
cout << endl;
}
return 0;
}

Tree Recovery UVA - 536的更多相关文章

  1. Tree(uva 536)

    先声明,我还在学习中,这个题大部分代码借鉴的大佬的,其实这算是比较经典二叉树题了,关键在于递归建树. 代码附上: #include <iostream> #include <cstr ...

  2. POJ 2255. Tree Recovery

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

  3. UVA 536 (13.08.17)

     Tree Recovery  Little Valentine liked playing with binary trees very much. Her favoritegame was con ...

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

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

  5. poj 2255 Tree Recovery 分治

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

  6. Tree Recovery POJ - 2255

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

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

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

  8. UVa 536 Tree Recovery | GOJ 1077 Post-order (习题 6-3)

    传送门1: https://uva.onlinejudge.org/external/5/536.pdf 传送门2: http://acm.gdufe.edu.cn/Problem/read/id/1 ...

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

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

随机推荐

  1. 使用PageHelper进行分页查询

    service层代码: public Result getDataSetList(String dataCode, String dataName, int pageIndex, int length ...

  2. vscode的代码片段

    一.快速创建一个vue单文件组件 "Create a new Component": { "prefix": "vue", "bo ...

  3. oracle impdp ORA-02304 invalid object identifier literal

    reference: https://webgeest.blogspot.com/2015/07/ora-39083-ora-02304-on-impdp-datapump.html     解决方法 ...

  4. 卧槽,好强大的魔法,竟能让Python支持方法重载

    1. 你真的了解方法重载吗? 方法重载是面向对象中一个非常重要的概念,在类中包含了成员方法和构造方法.如果类中存在多个同名,且参数(个数和类型)不同的成员方法或构造方法,那么这些成员方法或构造方法就被 ...

  5. 剑指 Offer 46. 把数字翻译成字符串 + 动态规划

    剑指 Offer 46. 把数字翻译成字符串 Offer_46 题目描述 题解分析 本题的解题思路是使用动态规划,首先得出递推公式如下 dp[i] = dp[i-1]+dp[i-2](如果s[i-1] ...

  6. Python 第三方登录 实现QQ 微信 微博 登录

    本人写的AgentLogin,能快速返回QQ.微信.微博第三方用户名信息,主要用于快速登录 用 pip命令安装 pip install AgentLogin 用法 : 导入这个包 from Agent ...

  7. MyBatis(九):MyBatis类型处理器(TypeHandler)详解

    TypeHandler简介 TypeHandler,顾名思义类型转换器,就是将数据库中的类型与Java中的类型进行相互转换的处理器. MyBatis 在设置预处理语句(PreparedStatemen ...

  8. SpringSecurity---基于内存的FormLogin

    SpringSecurity已经内置了一个登陆页面,所以目前我们就采取默认的登陆页面 一. 引入依赖 这步略过不表 二. 默认实现 添加接口 @RestController public class ...

  9. PTE 准备之 Read aloud

    Read aloud A text appears on screen.Read the text aloud rext up tp 60 words varies by task, dependin ...

  10. flutter资料

    Flutter社区和资源传送门 新: 慕课网<Flutter入门与案例实战>   |   中文网<Flutter实战>电子书 字体图标生成 http://fluttericon ...