思路:设只有一颗子树的节点有ans个设前序边历数组为pre[100],后序遍历数组为pos[100];前序遍历的第二个元素是A的一个子节点左右节点不知,设ax-ay表示一个树的前序遍历,bx-by表示后序遍历,可知如果pre[ax+1] = pos[i] 且 i = by-1,上一个根节点只有一个子树,此时令计数变量ans++;如果i != b2-1,很明显存在左右子树,此时应分别处理此时左子树为的前后序边历分别为:ax+1~ax+1+i-bx,bx~i,右子树为:ax+1+i-bx~ay,i+1~by-1;最后计算2^ans即为数的数目。值得注意的是:由于ans比较大,2^ans太大,需要用到大数乘法

 #include<iostream>
#include<algorithm>
#include<cstring> using namespace std;
typedef long long LL;
const int maxn = ;
int n, pre[maxn], pos[maxn], ans, index[maxn];
int a[maxn * ];
void dfs(int ax, int ay, int bx, int by)
{
if (ax >= ay)return;
int i = index[pre[ax + ]];
if (i == by - )ans++;
dfs(ax + , ax + + i - bx, bx, i);
dfs(ax + + i - bx + , ay, i + , by - );
}
int main()
{
ios::sync_with_stdio(false);
while (cin >> n) {
ans = ;
for (int i = ; i <= n; i++)cin >> pre[i];
for (int i = ; i <= n; i++) {
cin >> pos[i]; index[pos[i]] = i;
}
dfs(, n, , n);
memset(a, , sizeof(a));
a[] = ; n = ;
for (int i = ; i <= ans; i++) {
for (int j = ; j <= n; j++)
a[j] *= ;
for (int j = ; j <= n; j++)
if (a[j] >= ) {
a[j + ] = a[j + ] + a[j] / ;
a[j] %= ;
n = max(n, j + );
}
}
for (int i = n; i >= ; i--)
cout << a[i]; cout << endl;
}
return ;
}

51nod 1832 前序后序遍历的更多相关文章

  1. POJ 1240 Pre-Post-erous! && East Central North America 2002 (由前序后序遍历序列推出M叉树的种类)

    题目链接 问题描述 : We are all familiar with pre-order, in-order and post-order traversals of binary trees. ...

  2. POJ 1240 Pre-Post-erous! && East Central North America 2002 (由前序后序遍历序列推出M叉树的种类)

    题目链接:http://poj.org/problem?id=1240 本文链接:http://www.cnblogs.com/Ash-ly/p/5482520.html 题意: 通过一棵二叉树的中序 ...

  3. PAT 甲级 1043 Is It a Binary Search Tree (25 分)(链表建树前序后序遍历)*不会用链表建树 *看不懂题

    1043 Is It a Binary Search Tree (25 分)   A Binary Search Tree (BST) is recursively defined as a bina ...

  4. [itint5]根据前序后序遍历统计二叉树

    http://www.itint5.com/oj/#28 这题有意思.一开始还想不清楚,看了解释,很棒. 这个题目的特殊之处是所有节点的值都是不一样的. 所以递归过程可以大大简化. 先看两种遍历的性质 ...

  5. [Leetcode 105]*前序后序遍历形成树

    public TreeNode find(int[] preorder, int[] inorder,int j, int start, int end) { if (j > preorder. ...

  6. leetcode 105 106 从前序与中序遍历序列构造二叉树 从中序与后序遍历序列构造二叉树

    题目: 105 根据一棵树的前序遍历与中序遍历构造二叉树. 注意:你可以假设树中没有重复的元素. 例如,给出 前序遍历 preorder = [3,9,20,15,7] 中序遍历 inorder = ...

  7. 51nod 1832 先序遍历与后序遍历【二叉树+高精度】

    题目链接:51nod 1832 先序遍历与后序遍历 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 对于给定的一个二叉树的先序遍历和后序遍历,输出有多少种满足条件的 ...

  8. 【二叉树遍历模版】前序遍历&&中序遍历&&后序遍历&&层次遍历&&Root->Right->Left遍历

    [二叉树遍历模版]前序遍历     1.递归实现 test.cpp: 12345678910111213141516171819202122232425262728293031323334353637 ...

  9. POJ 2255 Tree Recovery(根据前序遍历和中序遍历,输出后序遍历)

    题意:给出一颗二叉树的前序遍历和中序遍历的序列,让你输出后序遍历的序列. 思路:见代码,采用递归. #include <iostream> #include <stdio.h> ...

随机推荐

  1. 七.Deque的应用案例-回文检查

    - 回文检测:设计程序,检测一个字符串是否为回文. - 回文:回文是一个字符串,读取首尾相同的字符,例如,radar toot madam. - 分析:该问题的解决方案将使用 deque 来存储字符串 ...

  2. win10上修改docker的镜像文件存储位置

    记住:修改的是docker从服务器上拉下来的镜像文件存储位置(本地),是不是镜像源地址(服务器) 首先 win10下的docker有可视化操作界面和命令行操作,下载了docker-ce.exe双击后就 ...

  3. oracle Transactional

    从shutdown transactional命令发布起, 禁止建立任何新的oracle连接. 从shutdown transactional命令发布起,禁止启动任何新的事务. 一旦数据库上所有的活动 ...

  4. select标签模拟placeholder属性与一般操作(最重要的是ios某一项被选中的兼容)

    1.为了统一样式,常常要模拟placeholder <select> <option disabled selected hidden>请选择</option> & ...

  5. DirectX11 With Windows SDK--07 添加光照与常用几何模型、光栅化状态

    原文:DirectX11 With Windows SDK--07 添加光照与常用几何模型.光栅化状态 前言 对于3D游戏来说,合理的光照可以让游戏显得更加真实.接下来会介绍光照的各种分量,以及常见的 ...

  6. spider csdn博客和quantstart文章

    spider csdn博客和quantstart文章 功能 提取csdn博客文章 提取quantstart.com 博客文章, Micheal Hall-Moore 创办的网站 特色功能就是: 想把原 ...

  7. 实用的cmd命令

    1.打开iis服务器:inetmgr 2.远程连接:mstsc 3.注册表:regedit.exe

  8. javascript中字符的一些常规操作

    1,获取第一个字符 var str = "hello word"; console.log(str[0]); // h 2,获取最后一个字符 var str = "hel ...

  9. SharpDX初学者教程第5部分:着色三角形

    原文 http://www.johanfalk.eu/blog/sharpdx-beginners-tutorial-part-5-coloring-the-triangle 在第4部分中,我们创建了 ...

  10. iOS中几种数据持久化方案:我要永远地记住你!

    http://www.cocoachina.com/ios/20150720/12610.html 作者:@翁呀伟呀 授权本站转载 概论 所谓的持久化,就是将数据保存到硬盘中,使得在应用程序或机器重启 ...