UVa 536 Tree Recovery
题意:给出一颗二叉树的前序遍历和中序遍历,输出其后序遍历
用杭电1710的代码改一点,就可以了。
#include<iostream>
#include<cstdio>
#include<cstring>
#include <cmath>
#include<algorithm>
using namespace std; typedef long long LL;
const int maxn=;
char a[maxn],b[maxn],ans[maxn],s1[maxn],s2[maxn]; void build(int n,char *s1,char *s2,char *s){
if(n<=) return;
int p;
for(int i=;i<n;i++){
if(s2[i]==s1[]){
p=i;//在中序遍历中找到根结点的位置
break;
}
} build(p,s1+,s2,s);//p为左子树的区间长度,s1+1是在前序遍历中左子树的开头,s2是在中序遍历中左子树的开头
build(n-p-,s1+p+,s2+p+,s+p);//n-p-1为右子树的区间长度,s1+p+1 是在前序遍历中右子树的开头,s2是在中序遍历中右子树的开头
s[n-]=s1[];
} int main()
{
int n,i;
while(cin>>a>>b){
for(i=;i<strlen(a);i++) s1[i]=a[i];
for(i=;i<strlen(b);i++) s2[i]=b[i];
n=strlen(a);
build(n,s1,s2,ans);
for(i=;i<n-;i++) printf("%c",ans[i]);
printf("%c\n",ans[i]);
}
return ;
}
UVa 536 Tree Recovery的更多相关文章
- 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/1 ...
- UVa 536 Tree Recovery(二叉树后序遍历)
Little Valentine liked playing with binary trees very much. Her favorite game was constructing rando ...
- 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 ...
随机推荐
- C#WinForm中播放背景音乐(亲测可用)
using System.Runtime.InteropServices; public static uint SND_ASYNC = 0x0001; public static uint SND_ ...
- 1257: [CQOI2007]余数之和sum - BZOJ
Description 给出正整数n和k,计算j(n, k)=k mod 1 + k mod 2 + k mod 3 + … + k mod n的值,其中k mod i表示k除以i的余数.例如j(5, ...
- Codeforces Round #359 (Div. 2) D. Kay and Snowflake 树的重心
题目链接: 题目 D. Kay and Snowflake time limit per test 3 seconds memory limit per test 256 megabytes inpu ...
- 【BZOJ】【3052】【WC2013】糖果公园
树分块 老早(大约一个月以前?)就听说这道神题了……orz rausen 一直拖到现在才做……发现还是不会呢= = 只好也去Orz了Hzwer和zky http://hzwer.com/5250.ht ...
- 【BZOJ】【1430】小猴打架
排列组合 蛮逗的…… 这题题干描述的就一股浓浓的Kruskal的气息……很容易就想到是求一个n个点的完全图的生成树个数,然后由于有序,再乘一个n-1的排列数(n-1条边的全排列)即(n-1)! 但是我 ...
- 【模板】Big-Step-Giant-Step 大步小步
求一个 的最小整数解 bsgs 当h是质数的时候使用 extbsgs 不满足上面那种情况的时候 具体参见http://tonyfang.is-programmer.com/posts/178997.h ...
- [转载]Windows 7 IIS (HTTP Error 500.21 - Internal Server Error)解决
今天在测试网站的时候,在浏览器中输入http://localhost/时,发生如下错误: HTTP Error 500.21 - Internal Server Error Handler " ...
- C/C++ 快速排序 quickSort
下面的动画展示了快速排序算法的工作原理. 快速排序图示:可以图中在每次的比较选取的key元素为序列最后的元素. #include <stdio.h> #include <stdlib ...
- C# 连接Oracle数据库
最近项目要用Oracle数据库,之前没搞过,近2天遇到好多问题,现在总结一下,做个备份. 一.关于Oracle安装 1.服务器端 从Oracle官网下载文件,file1和file2,解压之后安装就行了 ...
- 套题T8&T9
A - 8球胜负(eight) Time Limit:1000MS Memory Limit:65535KB 64bit IO Format:%lld & %llu Submi ...