算法竞赛入门经典 第二版 习题6-2 S树 S-Trees uva712
来源:互联网 发布:淘宝生意参谋标准版 编辑:程序博客网 时间:2024/04/20 11:52
题目:https://vjudge.net/problem/UVA-712
二叉树相关,只要题意能理解清楚就不难。
大致题意:给出一棵满二叉树,每层的(包括根节点)按顺序被编号为“x+数字”,之后的01字符串输入末端节点(叶子)从左到右权值。给出一些查询,0表示遇到节点向求左走,1表示遇到节点向右走,每个查询到达的叶子的值。如查询001表示遇到编号为x1节点向左走,遇到编号为x2的节点向左走,遇到编号为x3的节点向右走,最终到达叶子,返回对应叶子的值(0或1)。
思路:建树,每节点记下编号,用循环查询输出即可。
代码:C++
#include <iostream>#include <cstdio>#include <cstring>using namespace std;struct Node{ int value; Node *left; Node *right; Node(): left(NULL), right(NULL) {} Node(int v): value(v), left(NULL), right(NULL) {}};Node *root;int n;int order[10];string leaves;void build(Node *&nowroot, int depth, int &cnt){ if(depth!=n) { nowroot = new Node(order[depth]); build(nowroot->left, depth+1, cnt); build(nowroot->right, depth+1, cnt); } else { nowroot = new Node(int(leaves[cnt++]-'0')); }}int main(){ int T = 1; while(scanf("%d", &n) && n) { for(int i=0; i<n; i++) { scanf("%*c%*c%d", &order[i]); } cin >> leaves; int cnt = 0; build(root, 0, cnt); printf("S-Tree #%d:\n", T++); int m; cin >> m; while(m--) { string inquire; cin >> inquire; Node *t = root; for(int i=0; i<n; i++) { t = bool(inquire[t->value-1]-'0')?t->right:t->left; } printf("%d", t->value); } cout << endl << endl; } return 0;}
0 0
- 算法竞赛入门经典 第二版 习题6-2 S树 S-Trees uva712
- 算法入门经典习题6-2,uva712,S-Trees
- UVa712 习题6-2 S树(S-Trees)
- Uva712 S-Trees 【二分思想】【习题6-2】
- 习题6-2 S树 UVa712
- UVA712 S-Trees
- UVA712-S-Trees
- S-Trees UVA712
- uva712 - S-Trees
- UVa712 - S-Trees
- uva712 - S-Trees
- UVA712 - S-Trees
- uva712- S-Trees
- uva712 -- S-Trees
- S-Trees UVA712(二叉树)
- 算法竞赛入门经典 第二版 习题6-3 二叉树重建 Tree Recovery uva536
- 算法竞赛入门经典(第二版) 习题2-6 排列(permutation)
- 排列 permutation 算法竞赛入门经典第二版 习题2-6
- java泛型概要
- Spring-4,IOC容器
- 使用归并思想查找数组中的逆序对的数量——Java实现
- 1_简答的多项式运算
- HTML5基础--新的选择器收集
- 算法竞赛入门经典 第二版 习题6-2 S树 S-Trees uva712
- 巴什博弈
- 知识点
- 欧拉函数
- java解惑(一)—表达式之谜
- RecyclerView嵌套问题ScrollView
- 自定义View起步:自定义属性二
- 服务器、客户端实例(C#)
- 【数据结构与算法】带权有向图