51nod 1832 前序后序遍历

思路:设只有一颗子树的节点有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 前序后序遍历的更多相关文章
- 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. ...
- 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 题意: 通过一棵二叉树的中序 ...
- 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 ...
- [itint5]根据前序后序遍历统计二叉树
http://www.itint5.com/oj/#28 这题有意思.一开始还想不清楚,看了解释,很棒. 这个题目的特殊之处是所有节点的值都是不一样的. 所以递归过程可以大大简化. 先看两种遍历的性质 ...
- [Leetcode 105]*前序后序遍历形成树
public TreeNode find(int[] preorder, int[] inorder,int j, int start, int end) { if (j > preorder. ...
- leetcode 105 106 从前序与中序遍历序列构造二叉树 从中序与后序遍历序列构造二叉树
题目: 105 根据一棵树的前序遍历与中序遍历构造二叉树. 注意:你可以假设树中没有重复的元素. 例如,给出 前序遍历 preorder = [3,9,20,15,7] 中序遍历 inorder = ...
- 51nod 1832 先序遍历与后序遍历【二叉树+高精度】
题目链接:51nod 1832 先序遍历与后序遍历 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 对于给定的一个二叉树的先序遍历和后序遍历,输出有多少种满足条件的 ...
- 【二叉树遍历模版】前序遍历&&中序遍历&&后序遍历&&层次遍历&&Root->Right->Left遍历
[二叉树遍历模版]前序遍历 1.递归实现 test.cpp: 12345678910111213141516171819202122232425262728293031323334353637 ...
- POJ 2255 Tree Recovery(根据前序遍历和中序遍历,输出后序遍历)
题意:给出一颗二叉树的前序遍历和中序遍历的序列,让你输出后序遍历的序列. 思路:见代码,采用递归. #include <iostream> #include <stdio.h> ...
随机推荐
- swift 风骚的Guard语法
http://blog.csdn.net/pjk1129/article/details/48627153#0-qzone-1-64255-d020d2d2a4e8d1a374a433f596ad14 ...
- thinkphp5.0 验证提示信息的类型
以上是5.0.12版本 下面是5.0.5版本,没有elseif 上图中红方格的值只能是string类型,但是这种情况是在5.0.5版本是可以设置为array类型的
- List容器-ArrayList
特点: 有序重复,包括null,通过整数索引访问 实现类ArrayList和LinkedList ArrayList--动态数组 不线程同步 单线程合适 List<String> ...
- Apple的App Analytics统计平台你必须知道的
Apple最近在iTunesConnect里最新发布了App Analytics统计平台,提供了现有友盟统计平台和自有统计平台无法统计的数据,具有自己的独有特点,尤其是下面几个最让人头疼的流量分析转化 ...
- linux查看用户组所有成员
1.grep 'user1' /etc/group //找出用户组的gid user1:x:1004://得出gid=1004 2. awk -F":" '{print $1&qu ...
- nodeJs学习-03 GET数据请求,js拆解/querystring/url
原生JS解析参数: const http = require('http'); http.createServer(function(req,res){ var GET = {}; //接收数据容器 ...
- Vue.js 第2章 钩子函数&自定义指令&过滤器&计算属性&侦听器
目标 钩子函数 自定义指令 自定义过滤器 计算属性 监听属性 局部自定义指令 为什么需要自定义指令 为了复用,为了代码的灵活 指令的分类:全局指令,局部指令 在vm外面创建的指令 通过Vue.dire ...
- Linux 网络原理及基础设置
临时配置网络(ip,网关,dns)+永久配置 设置IP和掩码 ifconfig eth0 192.168.2.2 netmask 255.255.255.0 设置网关route add default ...
- auto uninstaller密钥激活码破解注册机ver 8.8.58
auto uninstaller密钥破解注册机ver 8.8.58 楼主分享几个auto uninstaller密钥破解注册机,可以用于auto uninstaller 8.8.58 .因为每个版本的 ...
- DataTable CAST 成集合后,进行自定义排序再转换回DataTable
dt = dt.Rows.Cast<DataRow>().OrderBy(r => Convert.ToInt32(r["数量"])==0?1:0).ThenBy ...