UVa 536 Tree Recovery | GOJ 1077 Post-order (习题 6-3)
传送门1: https://uva.onlinejudge.org/external/5/536.pdf
传送门2: http://acm.gdufe.edu.cn/Problem/read/id/1077
题意一样 输入不一样
HINT:
1. Preorder : (root, left subtree, right subtree);
Inorder : (left subtree, root, right subtree);
Postorder: (left subtree, right subtree, root);
2. 对于preorder,第一个元素即为整棵树的根
且在preorder中,待求子树结点靠前为根结点
3. 以找到的点为界,将inorder划分为两棵子树
#include <bits/stdc++.h>
using namespace std; string pre, in;
int length;
char ans[]; void process(int l, int r){
if(l > r) return;
bool flag = false;
char ch;
int mid;
for(int i = ; i < pre.length(); ++i){
for(int j = l; j <= r; ++j){
if(pre[i] == in[j]){
flag = true;
ch = pre[i];
mid = j;
break;
}
}
if(flag) break;
}
ans[--length] = ch; //注意先右后左(后序遍历从左到右再到根)
process(mid + , r);
process(l, mid - );
} int main(){
while(cin >> pre >> in){
length = pre.length();
process(, length - );
for(int i = ; i < pre.length(); ++i) cout << ans[i];
cout << endl;
}
return ;
}
另一种做法是建树模拟,贴上小光师兄博客里这道题的链接: http://blog.csdn.net/u012469987/article/details/41294313
UVa 536 Tree Recovery | GOJ 1077 Post-order (习题 6-3)的更多相关文章
- UVa 536 Tree Recovery(二叉树后序遍历)
Little Valentine liked playing with binary trees very much. Her favorite game was constructing rando ...
- UVa 536 Tree Recovery
题意:给出一颗二叉树的前序遍历和中序遍历,输出其后序遍历 用杭电1710的代码改一点,就可以了. #include<iostream> #include<cstdio> #in ...
- UVA 536 Tree Recovery 建树+不建树
题意: 给出先序和中序,求后序. 思路: ①建树然后递归输出. //建树的 #include<iostream> #include<cstdio> #include<qu ...
- UVA - 536 Tree Recovery (二叉树重建)
题意:已知先序中序,输出后序. #pragma comment(linker, "/STACK:102400000, 102400000") #include<cstdio& ...
- 【UVA】536 Tree Recovery(树型结构基础)
题目 题目 分析 莫名A了 代码 #include <bits/stdc++.h> using namespace std; string s1,s2; void buil ...
- POJ 2255. Tree Recovery
Tree Recovery Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11939 Accepted: 7493 De ...
- UVA 536 (13.08.17)
Tree Recovery Little Valentine liked playing with binary trees very much. Her favoritegame was con ...
- Tree Recovery(前序中序求后序)
Tree Recovery Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14640 Accepted: 9091 De ...
- poj 2255 Tree Recovery 分治
Tree Recovery Description Little Valentine liked playing with binary trees very much. Her favorite g ...
随机推荐
- JAVA-代理学习一之JDK实现
代理的实现依赖于反射,建议不太懂反射的童鞋先看看反射相关的知识点. 代理可以理解为对实际调用方法的一种能力的加强. 代理分为静态代理和动态代理: <1> 静态代理示例 接口MyInterf ...
- case a.ass_term_unit when '01' then (case a.ass_profit_mode when '0' then round(sum(a.ass_amount*a.ass_annual_rate/365*365*a.ass_term/100) ,2) when '1' then round(sum(a.ass_amount*a.ass_annual_rate/
--01 年 02 月 03 日 select a.ass_due_date, case a.ass_term_unit when '01' then (case a.ass_profit_mode ...
- python3中字典的copy
字典是可变的: first和second同时指向一个字典.first修改也会影响second.在程序中一定注意对字典参数的修改会对原始的字典进行修改.这也体现了字典是可变的. 字典的copy方法是浅拷 ...
- 【 VS 插件开发 】三、Vs插件简单功能的实现
[ VS 插件开发 ]三.Vs插件简单功能的实现
- GZIP压缩、解压缩工具类
GZIP压缩.解压缩工具类: public class GZIPUtiles { public static String compress(String str) throws IOExceptio ...
- Unity3DGUI:人物能量条
- ios监听ScrollView/TableView滚动的正确姿势
主要介绍 监测tableView垂直滚动的舒畅姿势 监测scrollView/collectionView横向滚动的正确姿势 1.监测tableView垂直滚动的舒畅姿势 通常我们用KVO或者在scr ...
- Java 内部类 this
内部类访问外部类的一个例子: public class Abc { private class Bc { public void print() { System.out.println(Abc.th ...
- ToString() 格式化
c# ToString() 格式化字符串 格式化数值:有时,我们可能需要将数值以一定的格式来呈现,就需要对数值进行格式化.我们使用格式字符串指定格式.格式字符串采用以下形式:Axx,其中 A 为格式 ...
- webstorm之js,css文件压缩
不说废话了,代码压缩是每个网站上线前的必备过程,但由于有时候小的项目或者加急的项目每次都构建一次类似gulp或者grunt等前端部署,实在是大题小做,所以才有了今天这个帖子: 我们会用到yui com ...