POJ 2255 Tree Recovery
阿新 • • 發佈:2017-09-03
future abcde left abc esc rac for not eal
This is an example of one of her creations:
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!
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.
For each test case, recover Valentine‘s binary tree and print one line containing the tree‘s postorder traversal (left subtree, right subtree, root).
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
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
Sample Input
DBACEGF ABCDEFG BCAD CBAD
Sample Output
ACBFGED CDAB
Source
Ulm Local 1997#include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<sstream> #include<algorithm> #include<queue> #include<deque> #include<iomanip> #include<vector> #include<cmath> #include<map> #include<stack> #include<set> #include<functional> #include<fstream> #include<memory> #include<list> #include<string> using namespace std; typedef long long LL; typedef unsigned long long ULL; #define N 31 #define INF 1000000009 #define eps 0.00000001 #define sf(a) scanf("%d",&a) const int MAXN = 1e5 + 3; char pre[N], mid[N]; int L[MAXN], R[MAXN]; int build(int l1, int r1, int l2, int r2)//l1 r1 是在前序序列的範圍 l2 r2 是中序的範圍 { if (l1 > r1 || l2 > r2) return -1; if (l1 == r1&&l2 == r2) return l1; char tr = pre[l1]; int p = l2; for (; p <= r2; p++) { if (mid[p] == tr) break; } L[l1] = build(l1 + 1, l1 + p - l2, l2, p - 1); R[l1] = build(l1 + p - l2 + 1, r1, p + 1, r2); return l1; } void print(int p) { if (L[p] != -1) print(L[p]); if (R[p] != -1) print(R[p]); printf("%c", pre[p]); } int main() { while (scanf("%s%s", pre, mid) != EOF) { memset(L, -1, sizeof(L)); memset(R, -1, sizeof(R)); int l = strlen(pre); int root = build(0, l - 1, 0, l - 1); print(root); printf("\n"); } }
POJ 2255 Tree Recovery