51nod 1832 先序遍历与后序遍历【二叉树+高精度】
两棵二叉树不同当且仅当对于某个x,x的左儿子编号不同或x的右儿子编号不同。
第一行一个正整数n(3<=n<=10000),表示二叉树的节点数,节点从1到n标号。
第二行n个整数a[i](1<=a[i]<=n),表示二叉树的先序遍历。
第三行n个整数b[i](1<=b[i]<=n),表示二叉树的后序遍历。
输出一个整数表示有多少种方案。保证至少有1种方案。
3
1 2 3
2 3 1
1 题解:找出只有一个儿子的节点数,则这种节点的儿子可以放在左子树或右子树即两种选择,然后将每种情况相乘即可,结果太大,再抄个高精度的板子嘛。
//yy:唉,我都快不记得怎么敲二叉树了。。类似的还可以拓展到n叉树,都是一类已知先序遍历和后序遍历求可构造树的方案数。。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define CLR(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long ll;
const int N = ;
struct BigInt
{
const static int mod = ;
const static int DLEN = ;
int a[],len;
BigInt()
{
memset(a,,sizeof(a));
len = ;
}
BigInt(int v)
{
memset(a,,sizeof(a));
len = ;
do
{
a[len++] = v%mod;
v /= mod;
}while(v);
}
BigInt(const char s[])
{
memset(a,,sizeof(a));
int L = strlen(s);
len = L/DLEN;
if(L%DLEN) len++;
int index = ;
for(int i = L-;i >= ;i -= DLEN)
{
int t = ;
int k = i - DLEN + ;
if(k < ) k = ;
for(int j = k;j <= i;j++)
t = t* + s[j] - '';
a[index++] = t;
}
}
BigInt operator +(const BigInt &b)const
{
BigInt res;
res.len = max(len,b.len);
for(int i = ;i <= res.len;i++)
res.a[i] = ;
for(int i = ;i < res.len;i++)
{
res.a[i] += ((i < len)?a[i]:)+((i < b.len)?b.a[i]:);
res.a[i+] += res.a[i]/mod;
res.a[i] %= mod;
}
if(res.a[res.len] > ) res.len++;
return res;
}
BigInt operator *(const BigInt &b)const
{
BigInt res;
for(int i = ; i < len; i++)
{
int up = ;
for(int j = ;j < b.len;j++)
{
int temp = a[i] * b.a[j] + res.a[i+j] + up;
res.a[i+j] = temp%mod;
up = temp/mod;
}
if(up != )
res.a[i + b.len] = up;
}
res.len = len + b.len;
while(res.a[res.len - ] == &&res.len > )
res.len--;
return res;
}
void output()
{
printf("%d",a[len-]);
for(int i = len-;i >= ;i--)
printf("%04d",a[i]);
printf("\n");
}
};
BigInt ans();
int pre[N], post[N];
int n;
void dfs(int l1, int r1, int l2, int r2) {
if(l1 > r1) return;
if(r1 - l1 == ) return;
l1++; r2--;
int num = ;
int p = l2;
while(post[p] != pre[l1]) p++;
int r11 = l1 + (p - l2 + ), r22 = p + ;
num++;
dfs(l1, r11, l2, r22);
if((r1-l1)-(p-l2+)!=) {
num++; dfs(r11, r1, p+, r2);
}
num = num == ? : ;
ans = ans * BigInt(num);
}
int main() {
int i, j;
scanf("%d", &n);
for(i = ; i < n; ++i) scanf("%d", &pre[i]);
for(i = ; i < n; ++i) scanf("%d", &post[i]);
dfs(,n,,n);
ans.output();
return ;
}
46ms
51nod 1832 先序遍历与后序遍历【二叉树+高精度】的更多相关文章
- 51Nod 算法马拉松28 A题 先序遍历与后序遍历 分治
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - 51Nod1832 题意概括 对于给定的一个二叉树的先序遍历和后序遍历,输出有多少种满足条件的二叉树. 两棵二 ...
- lintcode: 中序遍历和后序遍历树构造二叉树
题目 中序遍历和后序遍历树构造二叉树 根据中序遍历和后序遍历树构造二叉树 样例 给出树的中序遍历: [1,2,3] 和后序遍历: [1,3,2] 返回如下的树: 2 / \ 1 3 注意 你可 ...
- java编写二叉树以及前序遍历、中序遍历和后序遍历 .
/** * 实现二叉树的创建.前序遍历.中序遍历和后序遍历 **/ package DataStructure; /** * Copyright 2014 by Ruiqin Sun * All ri ...
- python数据结构之树和二叉树(先序遍历、中序遍历和后序遍历)
python数据结构之树和二叉树(先序遍历.中序遍历和后序遍历) 树 树是\(n\)(\(n\ge 0\))个结点的有限集.在任意一棵非空树中,有且只有一个根结点. 二叉树是有限个元素的集合,该集合或 ...
- 根据 中序遍历 和 后序遍历构造树(Presentation)(C++)
好不容易又到周五了,周末终于可以休息休息了.写这一篇随笔只是心血来潮,下午问了一位朋友PAT考的如何,顺便看一下他考的试题,里面有最后一道题,是关于给出中序遍历和后序遍历然后求一个层次遍历.等等,我找 ...
- TZOJ 3209 后序遍历(已知中序前序求后序)
描述 在数据结构中,遍历是二叉树最重要的操作之一.所谓遍历(Traversal)是指沿着某条搜索路线,依次对树中每个结点均做一次且仅做一次访问. 这里给出三种遍历算法. 1.中序遍历的递归算法定义: ...
- javascript数据结构与算法--二叉树遍历(后序)
javascript数据结构与算法--二叉树遍历(后序) 后序遍历先访问叶子节点,从左子树到右子树,再到根节点. /* *二叉树中,相对较小的值保存在左节点上,较大的值保存在右节点中 * * * */ ...
- LintCode2016年8月8日算法比赛----中序遍历和后序遍历构造二叉树
中序遍历和后序遍历构造二叉树 题目描述 根据中序遍历和后序遍历构造二叉树 注意事项 你可以假设树中不存在相同数值的节点 样例 给出树的中序遍历: [1,2,3] 和后序遍历: [1,3,2] 返回如下 ...
- 数据结构实习 - problem K 用前序中序建立二叉树并以层序遍历和后序遍历输出
用前序中序建立二叉树并以层序遍历和后序遍历输出 writer:pprp 实现过程主要是通过递归,进行分解得到结果 代码如下: #include <iostream> #include &l ...
随机推荐
- shiro入门与认证原理
一.shiro介绍 1.什么是shiro shiro是apache的一个开源框架,是一个权限管理的框架,实现 用户认证.用户授权. 2.shiro的优点 (1)shiro将安全认证相关的功能抽取出 ...
- Java入门系列-23-NIO(使用缓冲区和通道对文件操作)
NIO 是什么 java.nio全称java non-blocking(非阻塞) IO(实际上是 new io),是指jdk1.4 及以上版本里提供的新api(New IO) ,为所有的原始类型(bo ...
- 公司管理系列--80% of Your Culture is Your Founder(FaceBook)
80% of Your Culture is Your Founder When Molly Graham joined Facebook in 2008, the company still ...
- php mktime()函数
PHP提供一个很好用的函数mktime().你只要按顺序传送给mktime()你希望表示的小时,分钟,秒数,月份,日期,及年份,mktime()就会返回该日期自1970年1月1日的总秒数.获取2000 ...
- html中行级元素的居中显示。
垂直居中.以label标签为例. <style> #label1{ vertical-align:middle; line-height:40px;<*父元素的height*> ...
- Swift函数_inout参数
//无inout参数的函数 func changeName(var name:String){ name = "Hello" println(name) } let payerNa ...
- Java - 多线程中的不变性问题
这篇记录一下保证并发安全性的策略之——不变性. (注意:是Immutable,不是Invariant!) 将一连串行为组织为一个原子操作以保证不变性条件,或者使用同步机制保证可见性,以防止读到失效数据 ...
- 01.MD5加密
namespace _01.MD5加密 { class Program { static void Main(string[] args) { //MD5加密就是给想要的密码或者其它字符加密 //如果 ...
- Spring相关概念的理解理解
spring 框架的优点是一个轻量级比较简单易学的框架,实际使用中的有点优点有哪些呢!1.降低了组件之间的耦合性 ,实现了软件各层之间的解耦 2.可以使用容易提供的众多服务,如事务管理,消息服务等 3 ...
- CakePHP redirect函数
public function getContract($value=''){ App::uses ( 'UserContractController', 'Controller' ); $Contr ...