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 重做日志文件 ...
随机推荐
- SSLStrip 终极版 —— location 瞒天过海
之前介绍了 HTTPS 前端劫持 的方案,尽管非常有趣.然而现实却并不理想. 其唯一.也是最大的缺陷.就是无法阻止脚本跳转.若是没有这个缺陷,那就非常完美了 -- 当然也就没有必要写这篇文章了. 说究 ...
- 找了两个node+vue-cli+mogodb的小项目学学
实现的博客系统,自己抽时间搭建看看 https://www.cnblogs.com/yesyes/p/7188331.html https://github.com/chaohangz/vueBlog
- TCP/IP常见问题总结(二)
上一篇的传送门:TCP/IP常见问题总结(一) 6. TCP滑动窗体与回退N帧协议 TCP作为一个提供可靠服务的传输层协议,对于数据的发送必须拥有一套良好的反馈机制.让发送方得知接收方接收到了数据.而 ...
- 内容可编辑且随内容自增长的div
<!DOCTYPE HTML> <html> <head> <title></title> <meta http-equiv=&quo ...
- LINQ实现
public static IEnumerable<TSource> MyWhere<TSource>( this IEnumerable<TSource> ...
- session 生命周期
以前看到书上session 的生命周期,知道session的生命周期是在第一次访(即打开浏览器输入地址成功访问)的时候被创建.同时HttpSessionListener接口的sessionCreate ...
- Android系统定制----删除系统锁屏功能【转】
本文转载自:http://blog.csdn.net/morixinguan/article/details/56675914 frameworks/base/packages/SettingsPro ...
- Flask的配置文件 与 session
配置文件 flask中的配置文件是一个flask.config.Config对象(继承字典) 默认配置为: { 'DEBUG': get_debug_flag(default=False), 是否开启 ...
- Angular学习日记(一) 开发环境的搭建
从看视频到自己尝试,总会走很多的弯路才能成功. 首先:装最新版的nodejs. 第一种:使用npm安装 首先设置taobao镜像,npm config set registry https://reg ...
- 【转载】Java - Wait & Notify
[本文转自]http://www.cnblogs.com/dolphin0520/p/3920385.html 这三个方法的文字描述可以知道以下几点信息: 1)wait().notify()和noti ...