UVA - 536 Tree Recovery (二叉树重建)
2024-10-08 14:46:02
题意:已知先序中序,输出后序。
#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
typedef long long ll;
typedef unsigned long long llu;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const ll LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {, , -, , -, -, , };
const int dc[] = {-, , , , -, , -, };
const int MOD = 1e9 + ;
const double pi = acos(-1.0);
const double eps = 1e-;
const int MAXN = + ;
const int MAXT = + ;
using namespace std;
char pre_order[MAXN];
char in_order[MAXN];
int leftchild[MAXN];
int rightchild[MAXN];
int build(int L1, int R1, int L2, int R2){
if(L1 > R1) return ;
int root = pre_order[L1] - 'A' + ;
int st = L2;
while((in_order[st] - 'A' + ) != root) ++st;
int cnt = st - L2;
leftchild[root] = build(L1 + , L1 + cnt, L2, L2 + cnt - );
rightchild[root] = build(L1 + + cnt, R1, st + , R2);
return root;
}
void dfs(int root){
if(leftchild[root]) dfs(leftchild[root]);
if(rightchild[root]) dfs(rightchild[root]);
printf("%c", root + 'A' - );
}
int main(){
while(scanf("%s", pre_order) != EOF){
scanf("%s", in_order);
int len = strlen(pre_order);
build(, len - , , len - );
int root = pre_order[] - 'A' + ;
dfs(root);
printf("\n");
}
return ;
}
最新文章
- 微软“.Net社区虚拟大会”dotnetConf2015 第二天 无处不在的Xamarin
- ubutu之jdk安装
- wpf 在引用外部的资源字典
- CQOI2009中位数图
- 使用air进行移动app开发常见功能和问题(一)
- Swift中面向协议的编程
- Numerical Methods with MATLAB(1)
- Hadoop 中关于 map,reduce 数量设置
- JavaGUI版本销售管理系统
- poj3984迷宫问题(DFS广搜)
- 05-Eclispe配置Tomcat插件
- python读取excel,返回dic列表
- getservbyname和getservbyport
- Avito Cool Challenge 2018 自闭记
- 如何用Visual Studio 2013 (vs2013)编写C语言程序
- platforms
- [label][paypal] Paypal 支付页面的语言显示问题
- 【BZOJ1226】学校食堂(动态规划,状态压缩)
- curl下载文件
- HASH表的实现(拉链法)