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
/*
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;
}