UVA 536 (13.08.17)
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)的更多相关文章
- UVA 673 (13.08.17)
Parentheses Balance You are given a string consisting of parentheses () and []. Astring of this ty ...
- UVA 10194 (13.08.05)
:W Problem A: Football (aka Soccer) The Problem Football the most popular sport in the world (ameri ...
- UVA 253 (13.08.06)
Cube painting We have a machine for painting cubes. It is supplied withthree different colors: blu ...
- 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 ...
- UVA 10499 (13.08.06)
Problem H The Land of Justice Input: standard input Output: standard output Time Limit: 4 seconds In ...
- UVA 10025 (13.08.06)
The ? 1 ? 2 ? ... ? n = k problem Theproblem Given the following formula, one can set operators '+ ...
- UVA 465 (13.08.02)
Overflow Write a program that reads an expression consisting of twonon-negative integer and an ope ...
- UVA 10494 (13.08.02)
点此连接到UVA10494 思路: 采取一种, 边取余边取整的方法, 让这题变的简单许多~ AC代码: #include<stdio.h> #include<string.h> ...
- UVA 424 (13.08.02)
Integer Inquiry One of the first users of BIT's new supercomputer was Chip Diller. Heextended his ...
随机推荐
- spring Scurity终于测试OK了,复杂的功能还待深入研究!发布出来一起探讨吧!
spring Scurity终于测试OK了,复杂的功能还待深入研究!发布出来一起探讨吧! 就是因 为研究它,我的个天啦!头都大了一圈!还待修改完整版!我的目标不是每个项目拿到它就能使用!到时再说啦.. ...
- HDU 3697 Selecting courses(贪心)
题目链接:pid=3697" target="_blank">http://acm.hdu.edu.cn/showproblem.php?pid=3697 Prob ...
- DEV LookUpEdit 使用方法
public class field { public string Name { get; set; } public string Explain { get; set; } } List< ...
- (转)C#之玩转反射
前言 之所以要写这篇关于C#反射的随笔,起因有两个: 第一个是自己开发的网站需要用到 其次就是没看到这方面比较好的文章. 所以下定决心自己写一篇,废话不多说开始进入正题. 前期准备 在VS20 ...
- android 5.0 受欢迎的API简介
android 5.0 作为系统的一次重大升级,给用户和开发者带来了全新的体验.Material Design不但在视觉和操作上更胜一筹,扩展UI工具包同时也引入了大量新的API. 1. 3D视图和实 ...
- Java中书写要注意的地方
Java的命名规则:以字母.下划线(_)或$符号开头,其后跟任意数目的字母.数字.下划线和$符号. 注意: 数字不能作为标识符的开头: 除了下划线与$符号以外,其余的符号不能使用: 不能使用关键字作 ...
- 三、服务解析(Resolving Services)
当你完成组件注册,并将组件暴露为适当的服务后你就可以通过容器或者容器的子生命周期域来解析服务(After you have your components registered with approp ...
- unity碰撞组件、刚体组件
游戏导入标准资源包“Character Controllers”后可以为游戏对象添加 character(角色控制器)组件: 添加角色控制器组建以后可以控制游戏对象移动: 角色控制器组件因为与碰撞组件 ...
- CI框架uri去掉index.php
CI框架的入口是index.php,所以url实际上要多出一个index.php,非常不美观.我使用的是apache服务器,要开启mod_rewrite服务才可以. sudo a2enmod rewr ...
- MyISAM 存储引擎
在MYSQL 5.1 以及之前的版本,MyISAM 是默认的存储引擎.MyISAM 提供了大量的特性,包括全文索引,压缩,空间函数(gis)等,但是MyISAM不支持事务和行级锁,而且有一个毫无疑问的 ...