51Nod-1832-先序遍历与后序遍历
来源:互联网 发布:网站关键词优化智搜宝 编辑:程序博客网 时间:2024/05/21 19:26
ACM模版
描述
题解
官方题解的思路十分的清晰,所以先看看官方题解:
也就是说决定树的种类的是只拥有一个儿子的结点个数
代码
#include <cstdio>#include <cstring>using namespace std;const int MAX_LEN = 666;struct BigInt{ const static int MOD = 10000; const static int DLEN = 4; int len; int a[MAX_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 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"); }};const int MAXN = 10010;int n;int ct = 0;int a[MAXN];int b[MAXN];int get_id(int l, int r, int num){ for (int i = l; i < r; ++i) { if (b[i] == num) { return i; } } return -1;}int calc(int num){ if (num == 1) { return 1; } if (num == 2 || num == 0) { return 0; } return 0;}void dfs(int a_l, int a_r, int b_l, int b_r){ int len = a_r - a_l; if (len == 1) { return ; } a_l++; b_r--; int x = 0, cnt = a_r - a_l, id, ar, br; while (cnt != 0) { id = get_id(b_l, b_r, a[a_l]); ar = a_l + (id - b_l + 1); br = id + 1; x++; dfs(a_l, ar, b_l, br); cnt -= (id - b_l + 1); a_l = ar; b_l = id + 1; } ct += calc(x);}int main(){ 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, 0, n); BigInt res(1); for (int i = 0; i < ct; i++) { res = res * BigInt(2); } res.output(); return 0;}
阅读全文
0 0
- 51nod 1832 先序遍历与后序遍历
- 51Nod-1832-先序遍历与后序遍历
- 51nod 1832 先序遍历与后序遍历(DFS)
- 51nod 1832 先序遍历与后序遍历(树的遍历方法+高精度模板)
- 51 nod 算法马拉松28 先序遍历与后序遍历
- 算法马拉松28 先序遍历与后序遍历
- 先序遍历 中序遍历 后序遍历
- 树遍历方式总结:层次遍历、先序遍历、中序遍历、后序遍历
- 先序,中序,后序遍历
- 先序,中序,后序遍历
- 先序遍历/后序遍历创建线索二叉树
- 算法马拉松28 先序遍历与后序遍历(递归/高精度)
- 二叉树的先序遍历,后序遍历,中序遍历,层次遍历
- 二叉树的遍历(1)--先序遍历,中序遍历,后序遍历
- 二叉树 非递归 先序遍历 中序遍历 后序遍历 层次遍历
- hihocoder #1049 : 后序遍历 (已知先序遍历和中序遍历求后序遍历)
- 递归法遍历二叉树 (先序遍历 中序遍历 后序遍历)
- 树的遍历—先序遍历、中序遍历、后序遍历
- java面试基础2
- Python:正则表达式(三)-re模块的内容
- js中生成json对象
- Day-01
- .net core 借助容联云实现短信验证码
- 51Nod-1832-先序遍历与后序遍历
- POJ1995 Raising Modulo Numbers(快速幂取模,模板题)
- 没有上司的舞会 树形DP 经典题目
- 一个错误
- 30. Substring with Concatenation of All Words
- windows下搭建virtualenv、virtualenvwrapper虚拟环境
- Codeforces Round #430-01字典树&类异或最大值-D. Vitya and Strange Lesson
- jQuery怎么获取<c:forEach>标签的值
- select...for update