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 ...
随机推荐
- python学习-基础语法
字符编码 1.python 2.x 默认是ASCII 编码 不支持中文,所以在代码有中文的时候 需要在文件最上一行加上#coding=utf-8.python 3.x则没有该问题. 变量命名规则 1. ...
- 在代理中托管特殊方法的python代码实现
任务简单的介绍是: 在新风格对象模型中,Python操作其实是在类中查找特殊方法的(经典对象是在实例中进行操作的),现在需要将一些新风格的实例包装到代理中,,此代理可以选择将一些特殊的方法委托给内部的 ...
- vedio_note_1
同步复位 always @ (posedge clk) ....... 异步复位 always @ (posedge clk or negedge rst_n) ....... 异步复位和同步复位的优 ...
- Java 四舍五入并小数点后保存两位,千分位分隔
import java.text.DecimalFormat; public class FileTest { public static void main(String[] args) { ...
- php 中数据类型
总体划分 8 中 1基本类型(标量) 整型 int 整型的三种写法 <?php $n1 = 123; //10进制 $n2 = 0123; //8进制 $n3 = 0x123;//16进制 // ...
- js数组中的注意
1.数组删除 2.数组合并 3.原数组会被修改的数组方法有: 1)排序 .sotr() 2)逆序 .reverse() 3)数组拼接 .splice()
- HTTPClient网络异常:java.lang.IllegalStateException: Content has been consumed
在对代码进行重构时候,出现了一个异常,代码的网络请求使用的是HTTPClient: 但是其实代码中没有添加什么,只是添加了两句log: 后来发现是因为将EntityUtils.toString()方法 ...
- Ubuntu火狐、Chromium等浏览器安装flash插件
1.打开系统设置->软件和更新->其他软件,勾选Canonical合作伙伴,输入密码,重新载入更新 2.打开终端,按装插件 sudo apt install adobe-flashplug ...
- [SOJ] 商人的宣传
Description Bruce是K国的商人,他在A州成立了自己的公司,这次他的公司生产出了一批性能很好的产品,准备宣传活动开始后的第L天到达B州进行新品拍卖,期间Bruce打算将产品拿到各个州去做 ...
- C# 语言规范_版本5.0 (第14章 枚举)
1. 枚举 枚举类型 (enum type) 是一种独特的值类型(第 4.1 节),它用于声明一组命名的常量. 下面的示例 enum Color { Red, Green, Blue } 声明一个名为 ...