zoukankan      html  css  js  c++  java
  • Poj 2255 Tree Recovery

    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
    CDAB
    题意:已知先序和中序,求后序;;;
    参考代码:https://blog.csdn.net/liuke19950717/article/details/51291799

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    const int maxn=1005;
    char a[maxn],b[maxn];
    int x[maxn];//辅助数组
    void dfs(int la,int ra,int lb,int rb)
    {
        int i=x[a[la]-'A'];//定位节点的位置 
        int j=i-lb;//当前节点的左子树的字符长度 
        int k=rb-i;//当前节点的右子树的字符长度 
        if(j)
        {
            dfs(la+1,la+j,lb,i-1);//递归左子树
        }
        if(k)
        {
            dfs(la+j+1,ra,i+1,rb);//递归右子树
        }
        printf("%c",a[la]);//输出的次序就是后续遍历的结果 
    }
    int main()
    {
        while(~scanf("%s%s",a,b))
        {
            int len=strlen(a);
            for(int i=0;i<len;++i)
            {
                x[b[i]-'A']=i;
                //数组里保存中序的每个字母的下标 
            }
            dfs(0,len-1,0,len-1);
            printf("
    ");
        }
        return 0;
    }

    模仿大佬:

     1 #include<iostream>
     2 #include<string>
     3 #include<map>
     4 #include<algorithm>
     5 using namespace std;
     6 map<char, int > mp;
     7 string stra, strb;
     8 void dfs(int a, int b, int c, int d)
     9 {
    10     int i = mp[stra[a]];
    11     int j = i - c;
    12     int k = d - i;
    13     if (j)
    14     {
    15         dfs(a + 1, a + j, c, i - 1);
    16     }
    17     if (k)
    18     {
    19         dfs(a + j + 1, b, i + 1, d);
    20     }
    21     cout << stra[a];
    22 }
    23 int main()
    24 {
    25     while (cin >> stra >> strb)
    26     {
    27         int len = stra.length();
    28         mp.clear();
    29         for (int i = 0; i < len; i++)
    30             mp[strb[i]] = i;
    31         dfs(0, len - 1, 0, len - 1);
    32         cout << endl;
    33     }
    34     return 0;
    35 }

    //忧伤,原来二叉树的遍历还可以这么用,弄了两天的时间来弄懂别人代码的意思,,痛苦,不过收获也是蛮大的

  • 相关阅读:
    MSN、易趣、大旗被挂马 用户浏览后感染机器狗病毒 狼人:
    世界最大漏洞数据库发布最新研究报告 狼人:
    五角大楼最昂贵武器发展项目遭到黑客攻击 狼人:
    RSA呼吁厂商“创造性协作” 共同反击网络威胁 狼人:
    RSA2009:云计算服务如何保证安全? 狼人:
    黑客工具可将恶意软件隐藏于.Net框架 狼人:
    RSA安全大会将亮相25款热门安全产品 狼人:
    目录访问共享C#怎么访问共享目录
    代码下行Jquery结合Ajax和Web服务使用三层架构实现无刷新分页
    输出次数HDU2192:MagicBuilding
  • 原文地址:https://www.cnblogs.com/kangdong/p/9021320.html
Copyright © 2011-2022 走看看