USACO 3.4.3 American Heritage

American Heritage

Farmer John takes the heritage of his cows very seriously. He is not, however, a truly fine bookkeeper. He keeps his cow genealogies as binary trees and, instead of writing them in graphic form, he records them in the more linear `tree in-order' and `tree pre-order' notations.

Your job is to create the `tree post-order' notation of a cow's heritage after being given the in-order and pre-order notations. Each cow name is encoded as a unique letter. (You may already know that you can frequently reconstruct a tree from any two of the ordered traversals.) Obviously, the trees will have no more than 26 nodes.

Here is a graphical representation of the tree used in the sample input and output:

                  C
                /   \
               /     \
              B       G
             / \     /
            A   D   H
               / \
              E   F

The in-order traversal of this tree prints the left sub-tree, the root, and the right sub-tree.

The pre-order traversal of this tree prints the root, the left sub-tree, and the right sub-tree.

The post-order traversal of this tree print the left sub-tree, the right sub-tree, and the root.

PROGRAM NAME: heritage

INPUT FORMAT

Line 1:The in-order representation of a tree.
Line 2:The pre-order representation of that same tree.

SAMPLE INPUT (file heritage.in)

ABEDFCHG
CBADEFGH

OUTPUT FORMAT

A single line with the post-order representation of the tree.

SAMPLE OUTPUT (file heritage.out)

AEFDBHGC 
在POJ上刚写了一道类似的题,想起USACO上也有一个就过来给秒了。由先序遍历和中序遍历构建二叉树,然后在递归输出后序遍历。
/*
ID: xinming2
PROG: heritage
LANG: C++
*/
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <map>
#include <string>
#include <stack>
#include <cctype>
#include <vector>
#include <queue>
#include <set>
#include <utility>
#include <cassert>
using namespace std;
///#define Online_Judge
#define outstars cout << "***********************" << endl;
#define clr(a,b) memset(a,b,sizeof(a))
#define lson l , m  , rt << 1
#define rson m + 1 , r , rt << 1 | 1
#define mk make_pair
#define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++)
#define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++)
#define REP(i , x , n) for(int i = (x) ; i > (n) ; i--)
#define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--)
const int MAXN = 11000;
const int sigma_size = 26;
const long long LLMAX = 0x7fffffffffffffffLL;
const long long LLMIN = 0x8000000000000000LL;
const int INF = 0x7fffffff;
const int IMIN = 0x80000000;
#define eps 1e-8
const int MOD = (int)1e9 + 7;
typedef long long LL;
const double PI = acos(-1.0);
typedef double D;
typedef pair<int , int> pi;
///#pragma comment(linker, "/STACK:102400000,102400000")
struct Tree
{
    Tree* lc , *rc;
    char item;
};
char pre[30] , in[30];
Tree* buildtree(char *pre , char* in , int len)
{
    Tree* t;
    if(len <= 0)
    {
        t = NULL;
    }
    else
    {
        int num = 0;
        while(num < len && *(pre) != *(in + num))num++;
        t = (Tree *)malloc(sizeof(Tree));
        t -> item = *pre;
        t -> lc = buildtree(pre + 1 , in , num);
        t -> rc = buildtree(pre + (num + 1) , in + (num + 1) , len - (num + 1));
    }
    return t;
}
void print(Tree * t)
{
    if(t)
    {
        print(t -> lc);
        print(t -> rc);
        printf("%c" , t -> item);
    }
}
int main()
{
    freopen("heritage.in","r",stdin);
    freopen("heritage.out","w",stdout);
    while(scanf("%s%s" , in , pre) == 2)
    {
        int len = strlen(pre);
        Tree * t = buildtree(pre , in , len);
        print(t);
        puts("");
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值