POJ_2255_Tree Recovery
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 12342 | Accepted: 7712 |
Description
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 strings for each tree: a preorder traversal (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 the inorder traversal is ABCDEFG.
She thought that such a pair of strings would give enough information to reconstruct the tree later (but she never tried it).
Now, years later, looking again at the strings, she realized that reconstructing the trees was indeed possible, but only because she never had used the same letter twice in 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
Each test case consists of one line containing two strings preord and inord, representing the preorder traversal and inorder traversal of a binary tree. Both strings consist of unique capital letters. (Thus they are not longer than 26 characters.)
Input is terminated by end of file.
Output
Sample Input
DBACEGF ABCDEFG
BCAD CBAD
Sample Output
ACBFGED
CDAB3
题解:在前序中取根节点,在中序中找到该节点,其左边为该根节点左子树,右边为右子树
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
char fro[],mid[];
void beh(int fs,int fe,int ms,int me)
{
if(fs==fe)
{
cout<<fro[fs];
return;
}
if(fs>fe||ms>me)
return;
char root=fro[fs];
int i;
for(i=ms;i<me;i++)
if(root==mid[i])
break;
int len=i-ms;
beh(fs+,fs+len,ms,i-);
beh(fs+len+,fe,i+,me);
cout<<root;
}
int main()
{
while(scanf("%s%s",fro,mid)!=EOF)
{
int len=strlen(fro);
beh(,len-,,len-);
cout<<endl;
}
return ;
}
POJ_2255_Tree Recovery的更多相关文章
- POJ 2255. Tree Recovery
Tree Recovery Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11939 Accepted: 7493 De ...
- Android手机刷recovery
以前觉得android刷机是件很麻烦的事,现在倒不觉得了. 只要手机刷入第三方的recovery,一切都好办了,无论是root还是刷google play. recovery开源的有两大阵营,tw ...
- Change the Target Recovery Time of a Database (SQL Server) 间接-checkpoints flushcache flushcache-message
Change the Target Recovery Time of a Database (SQL Server) 间接checkpoints flushcache flushcache-mes ...
- SQL Server恢复软件 Stellar Phoenix sql recovery
SQL Server恢复软件 Stellar Phoenix sql recovery http://www.stellarinfo.com/ http://www.stellarinfo.com/ ...
- SQL Server恢复软件SysTools SQL Recovery/SysTools SQL Server Recovery Manager
SQL Server恢复软件SysTools SQL Recovery/SysTools SQL Server Recovery Manager http://www.systoolsgroup.co ...
- SQL Server通过File Header Page来进行Crash Recovery
SQL Server通过File Header Page来进行Crash Recovery 看了盖总的一篇文章 http://www.eygle.com/archives/2008/11/oracle ...
- DataBase异常状态:Recovery Pending,Suspect,估计Recovery的剩余时间
一,RECOVERY PENDING状态 今天修改了SQL Server的Service Account的密码,然后重启SQL Server的Service,发现有db处于Recovery Pendi ...
- Oracle Recovery 01 - 常规恢复之完全恢复
背景:这里提到的常规恢复指的是数据库有完备可用的RMAN物理备份. 实验环境:RHEL6.4 + Oracle 11.2.0.4 DG primary. 一.常规恢复之完全恢复:不丢失数据 1.1 单 ...
- Oracle Recovery 02 - 常规恢复之不完全恢复
背景:这里提到的常规恢复指的是数据库有完备可用的RMAN物理备份. 实验环境:RHEL6.4 + Oracle 11.2.0.4 单实例. 二.常规恢复之不完全恢复:部分数据丢失 2.1 重做日志文件 ...
随机推荐
- ubuntu下vi的使用
ubuntu下vi的使用 ssh之后对于server的文件,我习惯用gedit,可是不好改动,于是就用vi. 1.vi的基本概念 基本上vi能够分为三种状态,各自是命令模式(command mode) ...
- Linux系统调用过程分析
參考: <Linux内核设计与实现> 0 摘要 linux的系统调用过程: 层次例如以下: 用户程序------>C库(即API):INT 0x80 ----->system_ ...
- IOS-Storyboard控制器切换之Modal(1)
Modal模式是指模态切换.新开的界面会挡住之前的界面,使之不能获取焦点. 创建一个singleView模板的程序,打开storyboard文件.拖动2个UIViewController到界面中.按住 ...
- 使用bbed改动数据
bbed是一个强大的工具,同意我们绕过oracle直接从数据文件里改动相应的内容 ZBDBA@orcl11g>select * from emp; EMPNO ENAME JOB MGR HIR ...
- ios打地鼠游戏源代码
打地鼠游戏源代码,游戏是一款多关卡基于cocos2d的iPad打地鼠游戏源代码,这也是一款高质量的打地鼠游戏源代码,能够拥有逐步上升的关卡的设置,大家能够在关卡时设置一些商业化的模式来盈利的,很完美的 ...
- Estimating duration from bitrate, this may be inaccurate
通过 ffmpeg 获取 媒体 播放时长 ./ffmpeg-linux64-v3.3.1 -i /tmp/mp30001.mp3 ffmpeg version N-86111-ga441aa90e8 ...
- JeePlus:Maven 安装配置
ylbtech-JeePlus:Maven 安装配置 1.返回顶部 1. Maven 安装配置 1 Maven 由于Maven依赖Java运行环境,因此使用Maven之前需要配置Java的运行环境.下 ...
- 重装Eclipse 往其中加Python插件时 遇到不能独立运行c c++ python 代码时修改办法:
鼠标移动到新建项目处 ,右键->run as-> run configuration->选择Enable auto build 即可.
- 15_传智播客iOS视频教程_OC语言完全兼容C语言
OC支持C语言所有的运算符并且效果是一样的.C语言中所有的运算符OC都支持.这些所有的运算符OC当中全部都支持. 包括C语言的结构体.枚举全部都可以写在OC当中,没有任何问题,并且效果是一样的. 比如 ...
- P3469 [POI2008]BLO-Blockade(Tarjan 割点)
P3469 [POI2008]BLO-Blockade 题意翻译 在Byteotia有n个城镇. 一些城镇之间由无向边连接. 在城镇外没有十字路口,尽管可能有桥,隧道或者高架公路(反正不考虑这些).每 ...