51nod 1832 先序遍历与后序遍历(树的遍历方法+高精度模板)
来源:互联网 发布:个人域名如何备案 编辑:程序博客网 时间:2024/06/05 09:02
1832 先序遍历与后序遍历
基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题
收藏
关注
对于给定的一个二叉树的先序遍历和后序遍历,输出有多少种满足条件的二叉树。
两棵二叉树不同当且仅当对于某个x,x的左儿子编号不同或x的右儿子编号不同。
两棵二叉树不同当且仅当对于某个x,x的左儿子编号不同或x的右儿子编号不同。
Input
第一行一个正整数n(3<=n<=10000),表示二叉树的节点数,节点从1到n标号。第二行n个整数a[i](1<=a[i]<=n),表示二叉树的先序遍历。第三行n个整数b[i](1<=b[i]<=n),表示二叉树的后序遍历。
Output
输出一个整数表示有多少种方案。保证至少有1种方案。
Input示例
31 2 32 3 1
Output示例
1
产生方法数的情况就是左子树可以与右子树替换而不影响先序后序 这种情况只有这个节点只有一颗子树
这篇论文解释的很清楚https://wenku.baidu.com/view/a2a45aa0284ac850ad024261.html
#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 = 10005;struct BigInt{ const static int mod = 10000; const static int DLEN = 4; int a[600],len; BigInt() { memset(a,0,sizeof(a)); len = 1; } BigInt(int v) { memset(a,0,sizeof(a)); len = 0; do { a[len++] = v%mod; v /= mod; } while(v); } BigInt(const char s[]) { memset(a,0,sizeof(a)); int L = strlen(s); len = L/DLEN; if(L%DLEN) len++; int index = 0; for(int i = L-1; i >= 0; i -= DLEN) { int t = 0; int k = i - DLEN + 1; if(k < 0) k = 0; for(int j = k; j <= i; j++) t = t*10 + s[j] - '0'; a[index++] = t; } } BigInt operator +(const BigInt &b)const { BigInt res; res.len = max(len,b.len); for(int i = 0; i <= res.len; i++) res.a[i] = 0; for(int i = 0; i < res.len; i++) { res.a[i] += ((i < len)?a[i]:0)+((i < b.len)?b.a[i]:0); res.a[i+1] += res.a[i]/mod; res.a[i] %= mod; } if(res.a[res.len] > 0) res.len++; return res; } BigInt operator *(const BigInt &b)const { BigInt res; for(int i = 0; i < len; i++) { int up = 0; for(int j = 0; 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 != 0) res.a[i + b.len] = up; } res.len = len + b.len; while(res.a[res.len - 1] == 0 &&res.len > 1) res.len--; return res; } void output() { printf("%d",a[len-1]); for(int i = len-2; i >=0 ; i--) printf("%04d",a[i]); printf("\n"); }};BigInt ans(1);int a[N], b[N];void dfs(int l1, int r1,int l2, int r2){ if(r1-l1==0||l1>=r1) return ; l1++, r2--; int p=l2; while(b[p]!=a[l1]) p++; int cnt=1; dfs(l1,l1+(p-l2+1)-1,l2,p); if(r1-l1+1!=p-l2+1) { cnt=2; dfs(l1+(p-l2+1),r1,p+1,r2); } if(cnt==1) ans=ans*(2); return ;}int main(){ int n; scanf("%d", &n); for(int i=0;i<n;i++) scanf("%d", &a[i]); for(int i=0;i<n;i++) scanf("%d", &b[i]); dfs(0,n-1,0,n-1); ans.output(); return 0;}
阅读全文
0 0
- 51nod 1832 先序遍历与后序遍历(树的遍历方法+高精度模板)
- 51nod 1832 先序遍历与后序遍历
- 51Nod-1832-先序遍历与后序遍历
- 51nod 1832 先序遍历与后序遍历(DFS)
- 算法马拉松28 先序遍历与后序遍历(递归/高精度)
- 二叉树的遍历(1)--先序遍历,中序遍历,后序遍历
- 二叉树的遍历代码(先序遍历,中序遍历,后序遍历)
- 51 nod 算法马拉松28 先序遍历与后序遍历
- 模板-树的遍历(先序)
- c++实现二叉树的先序遍历,中序遍历,后序遍历(递归方法)
- 二叉树T 的先序遍历、中序遍历、后序遍历(递归实现)
- 二叉树的先序遍历,后序遍历,中序遍历,层次遍历
- 树的遍历—先序遍历、中序遍历、后序遍历
- 二叉树的遍历(2)--先序遍历,中序遍历,后序遍历(非递归)
- 算法学习 - 树的三种遍历(递归实现)先序遍历,中序遍历,后序遍历
- 树遍历方式总结:层次遍历、先序遍历、中序遍历、后序遍历
- java实现二叉树的构建以及三种遍历方法(先序遍历,中序遍历,后续遍历)
- 二叉树遍历(先序遍历、中序遍历、后序遍历)——递归方法和非递归方法
- Node.JS解析DNS(NodeJS开发实战详解)
- MQTT
- PowerShell日常管理系列——01Disable-Mailbox命令使用方法
- Java开发web的开发模式
- 最长公共子串(动态规划的思想)
- 51nod 1832 先序遍历与后序遍历(树的遍历方法+高精度模板)
- QT和MFC
- s2s4h5整合
- Java实现矩阵顺时针旋转90度
- [C/C++] C/C++语言文件读写函数
- [springcloud]eureka注册服务小案例
- 2017.9.9-2017.9.10---训练赛
- 基于centos的nginx安装配置
- 9月10日周日训练笔记