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 ...
随机推荐
- XMPP通讯开发-服务器好友获取以及监听状态变化
在 XMPP通讯开发-好友获取界面设计 我们设计了放QQ的列表功能,这里我们获取我们服务器上的 数据. 这一部分知识我们可以查看smack_3_3_0/smack_3_3_0/documentat ...
- HDU 5274(树链剖分)
树链剖分第一题QAQ,纪念下 #pragma comment(linker, "/STACK:102400000,102400000") #include <iostream ...
- linux服务器加入windows域时报错Ticket expired
[root@rusky]# net ads join -U administrator Enter administrator's password: kinit succeeded but ads_ ...
- oracle之replace结合substr的使用
select * from( SELECT TMM.ORDER_ID, TMM.IMPORT_ID, TMM.TMALL_ORDER_ID, TMM.MEMBER_NAME, TMM.ALIPAY_U ...
- c#、vb 自动属性
vb示例: Public Property Name() As String = "Bob" 等效于 Private _name As String = "Bob&quo ...
- C#调用VB6写的ActiveX Dll
搜索了很多资料,下载了Demo:http://download.csdn.net/detail/xieguoxian/2747484然后在同学电脑上测试才弄好...记录下 (一) 前期搜索资料: VB ...
- DEV GridControl 获取选中行的数据
private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowCha ...
- UIActivityIndicatorView活动控制器的大小改变
self.activityView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicat ...
- .net 调用Oracle.Data.Access 组件提供的用于批量操作的方法—获取数据库表结构方法和跟参数赋值方法
1./// <summary> /// 获取当前目标表结构 /// </summary> /// <param name="tableName"> ...
- ios swift(1)冒泡排序
//冒泡排序 稳定性最高 时间复杂度高 O(n(2)) ,交换次数太多, 一次交换等于三次赋值 最简单 var count = 0 func sortInts(inout data : [I ...