北大 ACM 2255 Tree Recovery
来源:互联网 发布:龙华行知实验小学排名 编辑:程序博客网 时间:2024/06/15 20:54
Tree Recovery
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 10152 Accepted: 6377
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:
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!
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.
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 ABCDEFGBCAD CBAD
Sample Output
ACBFGEDCDAB
#include <stdio.h>#include <stdlib.h>void f(char *inOrder, char *preOrder, int length){int elem, rootIndex = 0;if(length == 0) return ;elem = *preOrder;for(; rootIndex < length; rootIndex++)if(inOrder[rootIndex] == *preOrder) break;// leftf(inOrder, preOrder+1, rootIndex);// rightf(inOrder+rootIndex+1, preOrder+rootIndex+1, length-rootIndex-1);printf("%c", elem);return ;}int main(){char pre[27];char in[27];while(scanf("%s %s", pre, in) != EOF){f(in, pre, strlen(pre));printf("\n");}system("pause");return 0;}
- 北大 ACM 2255 Tree Recovery
- C语言 北大POJ 2255 Tree Recovery
- acm.jlu.edu.cn-1317-Tree Recovery
- poj 2255 Tree Recovery
- Tree Recovery---POJ--2255
- POJ 2255 Tree Recovery
- POJ 2255 Tree Recovery
- poj 2255 Tree Recovery
- POJ 2255: Tree Recovery
- POJ 2255 Tree Recovery
- POJ 2255 Tree Recovery
- POJ 2255 Tree Recovery
- POJ 2255 Tree Recovery
- poj 2255 Tree Recovery
- POJ 2255 Tree Recovery
- poj-2255-Tree Recovery
- poj 2255 Tree Recovery
- POJ 2255 Tree Recovery
- Apple Store iPhone 5s发货时间现更改为“1-2周”
- 优先队列 使用方法
- 精品软件-每日更新2013-1112
- ffmpeg结构体以及函数介绍(一)
- 南阳ACM7-街区最短路径问题
- 北大 ACM 2255 Tree Recovery
- mac osx 下的apt-get,yum的代替工具 ----homebrew
- 图形框架协议 Wayland
- latex公式中的空格如何表示?
- extern 的用法
- HDOJ 1001 Sum Problem
- 仅以此纪念一个正要走上“程序员”道路的“金融”从业者
- DataSnap开发:利用TParams进行多表事务更新
- java用补码表示负数