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 ...
随机推荐
- Python一日一练05----怒刷点击量
功能 自己主动获取CSDN文章列表,并对每篇文章添加点击量. 源代码 import urllib.request import re import time import random from bs ...
- UVA 825 Walking on the Safe Side(记忆化搜索)
Walking on the Safe Side Square City is a very easy place for people to walk around. The two-way ...
- setTimeout()和setInterval()小结
写在前面:在写H5游戏时经常需要使用定时刷新页面实现动画效果,比较常用即setTimeout()以及setInterval() setTimeout 描述 setTimeout(code,millis ...
- traceroute原理
traceroute原理 ICMP ICMP全称为Internet Control Message Protocol,即,网络控制报文协议. 当一个IP数据报发送失败时,最后一个路由器会向发送发传递一 ...
- Linux 统计代码行数命令
wc -l `find . -name '*.js'` wc -l `find . -regex ".*\.js"`
- 使用QTP打开应用程序的三种方法
1. systemUtil.Run ‘SystemUtil对象的Run方法 SystemUtil.Run “http://192.168.11.82/XXX” 参数实例: File:“http://1 ...
- python进行base64编解码
[转] 直接上代码 import base64 fin = open(r"D:\2.zip", "rb") fout = open(r"D:\2.x. ...
- 多线程中遇到ASSERT(pMap->LookupPermanent(hWndOrig) == NULL);怎么解决
XP下用VC开发的程序,在一个主线程调用3 个线程,线程之间要共享数据,结果总出现wincore.cpp line 980 ASSERT(pMap-> LookupPermane ...
- Injector Job深入分析
Injector Job的主要功能是根据crawlId在hbase中创建一个表,将将文本中的seed注入表中. (一)命令执行 1.运行命令 [jediael@master local]$ bin/n ...
- CMS设计-组件化
原来CMS使用的专题类的页面是 : 事先由前端写好完整页面,再交付给运营使用,这样使用的比较灵活,可以根据市场的不同需求由前端切出不同的页面,有时候一旦需求过多,就耽误切图的时间. 现在M和H5采用组 ...