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 ...
随机推荐
- 配置linux中文
1.~/.bash_profile文件添加一下内容并执行source ~/.bash_profile export NLS_LANG=AMERICAN_AMERICA.ZHS16GBK 2./etc ...
- AngularJS初步
AngularJS特点 遵循AMD规范 不需要操作节点 对于jquery,一般是利用现有完整的DOM,然后在这戏Dom的基础上进行二次调教了:而对于AngularJS等框架则是根据数据模型以及其对应用 ...
- WebApi2官网学习记录---批量处理HTTP Message
原文:Batching Handler for ASP.NET Web API 自定义实现HttpMessageHandler public class BatchHandler : HttpMess ...
- cookie 的Domain删除失败的问题
最近接手一个老项目,项目中使用的是cookie来做的处理的,新增的时候cookie添加了域, 但是删除的时候没有添加域,导致删除cookie的时候一直失败!还有cookie的创建与删除,应该都必需经过 ...
- OWIN初探(转)
什么是 OWIN ? OWIN 的全称是 "Open Web Interface for .NET", OWIN 在 .NET Web 服务器和 .NET Web 应用之间定义了一 ...
- html的空格显示距离问题
一.使用全角空格 全角空格被解释为汉字,所以不会被被解释为HTML分隔符,可以按照实际的空格数显示. 二.使用空格的替代符号 替代符号就是在需要显示空格的地方加入替代符号,这些符号会被浏览器解释为空格 ...
- Sql Server根据表名获得所有列及其属性
select a.name columnname,c.name as typename,case when a.is_nullable =0 then 'Not Null' else 'Null' e ...
- oracle 存储过程返回结果集 (转载)
好久没上来了, 难道今天工作时间稍有空闲, 研究了一下oracle存储过程返回结果集. 配合oracle临时表, 使用存储过程来返回结果集的数据读取方式可以解决海量数据表与其他表的连接问题. 在存储过 ...
- Django初学笔记1.
1,安装python 和 Django , 参考网上教程(安装python,配置path, 安装django,配置path..) 2,查看django安装版本:cmd-->python--> ...
- JasperReport使用心得
1. JasperReport 报表文件视图化生成工具iReport. iReport做为一个生成JasperReport的视图工具,和我们是使用的大多数报表创建工具没有太大的差别,都是拖控件,搭出报 ...