POJ_2255_Tree Recovery

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[27],mid[27];
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+1,fs+len,ms,i-1);
    beh(fs+len+1,fe,i+1,me);
    cout<<root;
}
int main()
{
    while(scanf("%s%s",fro,mid)!=EOF)
    {
        int len=strlen(fro);
        beh(0,len-1,0,len-1);
        cout<<endl;
    }
    return 0;
}
时间: 2024-11-08 20:26:40

POJ_2255_Tree Recovery的相关文章

记录一次ceph recovery经历

一次ceph recovery经历 背景 这是一个测试环境. 该环境中是cephfs 一共12个节点, 2个client.2个mds.8个osd mds: 2颗CPU,每个4核,一共是8核. 128G内存, 单独的两个节点,只作为mds cpu型号: Intel(R) Xeon(R) CPU E5-1620 v3 @ 3.50GHz osd节点, 每个24核, 8 × 4T SATA盘, 1 SSD:INTEL SSD SC2BB48 (480G) 64G内存 cpu型号: Intel(R) X

关于RECOVERY清除数据的分析

[前言] 讨论:双清和清空所有数据的问题 说明:以前写的帖子都写三清,那个是为了保险起见才叫大家三项清除,毕竟人都有刚开始的时候,但看了郭贤普的帖子<系统与数据兼容性测试>之后,我觉得有必要说说这个问题,顺便说说双系统共存的时候为什么切换系统要清空所有数据. [分析内容] 分析关键名词:清空缓存.清空用户数据.清空所有数据[点这里是郭贤普帖子] 一.清空缓存 安卓手机的cache(缓存),如果是计算机它的功能就是CPU与内存数据交换的一个中介存储,但是在安卓手机上它也是一样的道理,就如郭贤普所

Solr4.8.0源码分析(22)之 SolrCloud的Recovery策略(三)

Solr4.8.0源码分析(22)之 SolrCloud的Recovery策略(三) 本文是SolrCloud的Recovery策略系列的第三篇文章,前面两篇主要介绍了Recovery的总体流程,以及PeerSync策略.本文以及后续的文章将重点介绍Replication策略.Replication策略不但可以在SolrCloud中起到leader到replica的数据同步,也可以在用多个单独的Solr来实现主从同步.本文先介绍在SolrCloud的leader到replica的数据同步,下一篇

ORA-01153 an incompatible media recovery is active

ORA-01153错误处理 问题描述: 主备在做Switchover切换时,在切换后的备库报如下错误: ORA-01153: an incompatible media recovery is active 解决办法: 对standby database 进行手动应用archive log SQL> recover managed standby database cancel; SQL> recover automatic standby database ; SQL> RECOVER

ORA-01113: file xxxx needs media recovery

由于规范存储位置以及存储空间调整缘故需要移动表空间MRP_INDEX2的数据文件,如下所示,首先将表空间MRP_INDEX2脱机; 然后复制数据文件:接着重命名数据文件,最后将表空间MRP_INDEX2联机. 在操作过后,最后一步将表空间MRP_INDEX2联机上线时,出现了意外错误信息,如下所示: SQL> ALTER TABLESPACE MRP_INDEX2 OFFLINE NORMAL;   Tablespace altered.   SQL> !cp /u03/flash_recov

I.MX6 recovery mode hacking

/******************************************************************************** * I.MX6 recovery mode hacking * 说明: * 看一下i.MX6 Recovery模式是怎么工作的. * * 2017-6-12 深圳 龙华樟坑村 曾剑锋 ****************************************************************************

SMON: Parallel transaction recovery tried 引发的问题--转载

SMON: Parallel transaction recovery tried 这个一般是在具有在跑大数据量的 transaction的时候kill掉了进程而导致 smon 去清理 回滚段时导致的. 这个在业务高峰期的时候,如果发现这个,有可能导致 SMON 占用了 100% cpu 而导致 系统 hang 在那边.即使你shutdown immediate ,oracle 也会等待 smon 清理完毕才能关机,而这个等待过程也许是漫长的.如果你 shutdown abort,那么oracl

如何使Recovery分区正常工作

通常安装完系统后,在进入Clover菜单选择Recovery分区后是进不去的,对于我这种完美强迫症患者来说这是不能忍的,最后,终于在网上找到个简单办法让它工作,废话不多说,上命令: 先找到Recovery分区的磁盘号 diskutil list /dev/disk0 (internal, physical): #: TYPE NAME SIZE IDENTIFIER 0: GUID_partition_scheme *256.1 GB disk0 1: EFI EFI 209.7 MB disk

android JNI库实现reboot,recovery

1.recovery函数: #define UPDATE_TITLE "--update_package=" #define UPDATE_COMMAND_FILE "/cache/recovery/command" #define UPDATE_FLAG_FILE "/cache/recovery/last_flag" #define LAST_INSTALL_FILE "/cache/recovery/last_install&qu