POJ 2255 Tree Recovery(递归)
来源:互联网 发布:幻影2000和歼20 知乎 编辑:程序博客网 时间:2024/06/08 19:43
Tree Recovery
Time Limit: 1000MSMemory Limit: 65536KTotal Submissions: 15683Accepted: 9677
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
Source
Ulm Local 1997
[Submit] [Go Back] [Status] [Discuss]
题意
就是给你两串字符串,第一串表示一颗树的先序遍历,第二串表示一棵树的中序遍历,让你求出这个树的后序遍历。
思路
因为先序遍历是先根,后左子树,最后右子树,而中序遍历则是先左子树,再根,最后右子树,所以我们可以递归处理,对于在先序遍历里找到的点,我们在中序遍历里找到它的位置,然后将现在这个字符串分成左右两部分,进行递归处理,直到只剩最后一个字符就行了。
Code
#pragma GCC optimize(3)#include<iostream>#include<algorithm>#include<cstring>#include<cstdio>#include<cctype>#include<climits>#include<cstdlib>#include<cmath>#include<queue>#include<stack>#include<climits>#include<vector>using namespace std;typedef long long ll;inline void readInt(int &x) { x=0;int f=1;char ch=getchar(); while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();} while(isdigit(ch))x=x*10+ch-'0',ch=getchar(); x*=f;}inline void readLong(ll &x) { x=0;int f=1;char ch=getchar(); while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();} while(isdigit(ch))x=x*10+ch-'0',ch=getchar(); x*=f;}/*================Header Template==============*/string pre,mid,ans;int len;inline void solve(int pos,string x) { len=x.length(); if(len==0) return; if(len==1) { ans+=x[0]; return; } char now=pre[pos]; int p=x.find(now); string r=x.substr(p+1,len-p),l=x.substr(0,p); solve(pos+1,l); solve(pos+p+1,r); ans+=now;}int main() { while(cin>>pre>>mid) { ans=""; solve(0,mid); cout<<ans<<'\n'; }}
阅读全文
0 0
- POJ 2255 Tree Recovery(递归)
- POJ:2255 Tree Recovery(递归||树)
- POJ 2255 Tree Recovery(递归)
- poj 2255 Tree Recovery 递归
- Poj 2255-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
- 拼接的变量可以作为php函数名
- 浅析CentOS和RedHat Linux的区别
- comparator使用案例
- 更新层级和层级编码
- 原始GBDT推导
- POJ 2255 Tree Recovery(递归)
- Python爬虫-爬取腾讯小视频
- 算法系列--荷兰国旗问题
- 阿里云面向企业效率的云上产品全解析:云桌面、云AP、云投屏
- opencv3.2.0+opencv_contrib-3.2.0+vs2015开发配置
- 软件报价指南
- 线程的两种创建方式及比较
- CleanMyMac卸载不干净?
- 9-函数定义及调用