Tree Recovery 

Little Valentine liked playing with binary trees very much. Her favoritegame was constructingrandomly 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 stringsfor each tree: a preordertraversal (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 theinorder traversal isABCDEFG.

She thought that such a pair of strings would give enough information toreconstruct the tree later (but she never tried it).

Now, years later, looking again at the strings, she realized thatreconstructing the trees was indeedpossible, but only because she never had used the same letter twicein 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 Specification

The input file will contain one or more test cases.Each test case consists of one line containing two strings preord andinord, representing thepreorder traversal and inorder traversal of a binary tree. Both stringsconsist of unique capitalletters. (Thus they are not longer than 26 characters.)

Input is terminated by end of file.

Output Specification

For each test case, recover Valentine's binary tree and print one linecontaining the tree's postordertraversal (left subtree, right subtree, root).

Sample Input

DBACEGF ABCDEFG
BCAD CBAD

Sample Output

ACBFGED
CDAB

题意: 给出前序和中序, 求后序

做法: 递归建树, 刘汝佳<算法竞赛-入门经典>中的 二叉树重建 部分有讲

AC代码:

#include<stdio.h>
#include<string.h> void build(int n, char *s1, char *s2, char *s) {
if(n <= 0)
return;
int p = strchr(s2, s1[0]) - s2;
build(p, s1+1, s2, s);
build(n-1-p, s1+p+1, s2+p+1, s+p);
s[n-1] = s1[0];
} int main() {
char str1[30], str2[30];
char ans[30];
int len;
while(scanf("%s%s", str1, str2) != EOF) {
len = strlen(str1);
build(len, str1, str2, ans);
ans[len] = '\0';
puts(ans);
}
return 0;
}

UVA 536 (13.08.17)的更多相关文章

  1. UVA 673 (13.08.17)

     Parentheses Balance  You are given a string consisting of parentheses () and []. Astring of this ty ...

  2. UVA 10194 (13.08.05)

    :W Problem A: Football (aka Soccer)  The Problem Football the most popular sport in the world (ameri ...

  3. UVA 253 (13.08.06)

     Cube painting  We have a machine for painting cubes. It is supplied withthree different colors: blu ...

  4. UVA 573 (13.08.06)

     The Snail  A snail is at the bottom of a 6-foot well and wants to climb to the top.The snail can cl ...

  5. UVA 10499 (13.08.06)

    Problem H The Land of Justice Input: standard input Output: standard output Time Limit: 4 seconds In ...

  6. UVA 10025 (13.08.06)

     The ? 1 ? 2 ? ... ? n = k problem  Theproblem Given the following formula, one can set operators '+ ...

  7. UVA 465 (13.08.02)

     Overflow  Write a program that reads an expression consisting of twonon-negative integer and an ope ...

  8. UVA 10494 (13.08.02)

    点此连接到UVA10494 思路: 采取一种, 边取余边取整的方法, 让这题变的简单许多~ AC代码: #include<stdio.h> #include<string.h> ...

  9. UVA 424 (13.08.02)

     Integer Inquiry  One of the first users of BIT's new supercomputer was Chip Diller. Heextended his ...

随机推荐

  1. android 点击桌面图标,打开手机浏览器进入对应的站点

    做一个假的adnroid app.要实现点击桌面图标.打开手机浏览器进入对应的站点,实现方法非常easy import android.app.Activity; import android.con ...

  2. Android--获取当前系统的语言环境

    private boolean isZh() {        Locale locale = getResources().getConfiguration().locale;         St ...

  3. install-file -Dfile=J:\project01\workspace\service\lib\javapns-jdk16-163.jar -DgroupId=org.json -Dar

    今天在开发项目的时候发现了一个问题,所以通过博客来记录起来! 为了以后在问题的解决方面能得到借鉴! 问题的现象是这种: 这样会报错的.pom.xml文件他在编译.检查他的文件语法的时候是须要參考库中的 ...

  4. Citrix 服务器虚拟化之九 Xenserver虚拟机的XenMotion

    Citrix 服务器虚拟化之九 Xenserver虚拟机的XenMotion XenMotion 是 XenServer 的一项功能,能够将正在运行的虚拟机从一台 XenServer 主机上迁移到另外 ...

  5. Android基于WIFI实现电脑和手机间数据传输的技术方案研究

    Android手机和电脑间基于wifi进行数据传输,从技术上讲,主要有两种方案: 一种是通过ftp协议实现,Android手机作为数据传输过程中的ftp服务器: 一种是通过http协议实现.Andro ...

  6. top 命令SQLServer-sybase-oracle

    SQLServer: select top 10 * from tablename; select top 10 percent from tablename; select * from table ...

  7. mp4文件格式之fragment mp4

    目前网络上对mp4文件格式的总结已经相当多了,我就不在这里抄别人总结的东西了,想入门的话百度一下会有许多这方面的文章.这里介绍一下其他文章中很少涉及的一种mp4文件结构,即fragment mp4,也 ...

  8. C# KeyValuePair<TKey,TValue>与Container

    KeyValuePair<TKey,TValue>  KeyValuePair<TKey,TValue>是一个结构体,相当于C#一个Map类型的对象,可以通过它记录一个键/值对 ...

  9. silverlight visifire控件图表制作——silverlight 静态页面xaml

    一.silverlight 静态页面 1. 时间控件:DatePicker ,添加引用: xmlns:sdk="clr-namespace:System.Windows.Controls;a ...

  10. 将 varchar 值转换为 JDBC 数据类型 DATE 时发生错误。

    问题是: 我是这样解决的  : 网上的 转型方法 并不好使 ,我想了想 可能是由于返回值是String  我 就成功的解决错误了  ..下面是关于原理的讲解肯定方法不唯一   至于错误,的产生,这个肯 ...