Tree Recovery
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 12342   Accepted: 7712

Description

Little Valentine liked playing with binary trees very much. Her favorite game was constructing randomly 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 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

The input will contain one or more test cases. 
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

For each test case, recover Valentine's binary tree and print one line containing the tree's postorder traversal (left subtree, right subtree, root).

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的更多相关文章

  1. POJ 2255. Tree Recovery

    Tree Recovery Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11939   Accepted: 7493 De ...

  2. Android手机刷recovery

    以前觉得android刷机是件很麻烦的事,现在倒不觉得了.  只要手机刷入第三方的recovery,一切都好办了,无论是root还是刷google play.  recovery开源的有两大阵营,tw ...

  3. 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 ...

  4. SQL Server恢复软件 Stellar Phoenix sql recovery

    SQL Server恢复软件 Stellar Phoenix sql recovery http://www.stellarinfo.com/ http://www.stellarinfo.com/ ...

  5. SQL Server恢复软件SysTools SQL Recovery/SysTools SQL Server Recovery Manager

    SQL Server恢复软件SysTools SQL Recovery/SysTools SQL Server Recovery Manager http://www.systoolsgroup.co ...

  6. SQL Server通过File Header Page来进行Crash Recovery

    SQL Server通过File Header Page来进行Crash Recovery 看了盖总的一篇文章 http://www.eygle.com/archives/2008/11/oracle ...

  7. DataBase异常状态:Recovery Pending,Suspect,估计Recovery的剩余时间

    一,RECOVERY PENDING状态 今天修改了SQL Server的Service Account的密码,然后重启SQL Server的Service,发现有db处于Recovery Pendi ...

  8. Oracle Recovery 01 - 常规恢复之完全恢复

    背景:这里提到的常规恢复指的是数据库有完备可用的RMAN物理备份. 实验环境:RHEL6.4 + Oracle 11.2.0.4 DG primary. 一.常规恢复之完全恢复:不丢失数据 1.1 单 ...

  9. Oracle Recovery 02 - 常规恢复之不完全恢复

    背景:这里提到的常规恢复指的是数据库有完备可用的RMAN物理备份. 实验环境:RHEL6.4 + Oracle 11.2.0.4 单实例. 二.常规恢复之不完全恢复:部分数据丢失 2.1 重做日志文件 ...

随机推荐

  1. react 组件之间的通信

    react推崇的是单向数据流,自上而下进行数据的传递,但是由下而上或者不在一条数据流上的组件之间的通信就会变的复杂.解决通信问题的方法很多,如果只是父子级关系,父级可以将一个回调函数当作属性传递给子级 ...

  2. Java 实现一个链表

    public class MyList { static class Node {// 节点类 Object data; Node next; public Node(Object data) {// ...

  3. LVS 负载均衡 (VS/DR模式 与 VS/TUN 模式)

    一.VS/DR模式 ①.客户端将请求发往前端的负载均衡器,请求报文源地址是CIP,目标地址为VIP. ②.负载均衡器收到报文后,发现请求的是在规则里面存在的地址,那么它将目标MAC改为了RIP的MAC ...

  4. C# 插件热插拔 .NET:何时应该 “包装异常”? log4.net 自定义日志文件名称

    C# 插件热插拔   所谓热插拔就是插件可以 在主程序不重新启动的情况直接更新插件, 网上有很多方案: https://www.cnblogs.com/happyframework/p/3405811 ...

  5. String类的四个默认成员函数

    优化版的拷贝构造函数,先创建一个暂时实例tmp,接着把tmp._ptr和this->_ptr交换,因为tmp是一个局部变量.程序执行到该函数作用域外,就会自己主动调用析构函数.释放tmp._pt ...

  6. Wordpress3.9开启多网站配置配置nginx进行局域网測试.

    由于须要帮staff迁移一些数据, 所以想到了使用wordpress的多网站. 这个功能在wordpress3.0后就有了. 软件系统等信息:  OS: linux debian wheezy php ...

  7. Delphi异常处理的基本原则和方法

    Delphi异常处理的基本原则和方法 一.异常的来源. 在Delphi的应用程序中,下列的情况都比较有可能产生异常.(1)文件处理(2)内存分配(3)Windows资源(4)运行时创建对象和窗体(5) ...

  8. HDU 5754Life Winner Bo

    Life Winner Bo Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) ...

  9. react-native 项目更名步骤

    刚开始开发项目的时候,更没有想好一个项目名称,如何才能更名一个RN APP名称呢,可按照如下方式操作即可. 使用说明 更改package.json { "name": " ...

  10. ASP.NET调用存储过程并接收存储过程返回值

    ASP.NET调用存储过程并接收存储过程返回值 2010-08-02 11:26:17|  分类: C#|字号 订阅       2010年02月27日 星期六 23:52 假设表结构Create T ...