算法竞赛入门经典 第二版 习题6-2 S树 S-Trees uva712
2017-04-10 19:37
429 查看
题目: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;
}
二叉树相关,只要题意能理解清楚就不难。
大致题意:给出一棵满二叉树,每层的(包括根节点)按顺序被编号为“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;
}
相关文章推荐
- 算法竞赛入门经典 第二版 习题9-2 免费糖果 Free Candies uva10118
- 算法入门经典习题6-2,uva712,S-Trees
- 算法竞赛入门经典 第二版 习题5-2 Ducci序列 Ducci Sequence uva1594
- 算法竞赛入门经典 第二版 例题11-4 电话圈 Calling Circles uva247
- 算法竞赛入门经典第四章习题4-7 RAID! UVA - 509
- 【算法竞赛入门经典第二版学习】第三章习题
- 算法竞赛入门经典第四章习题4-8 Extraordinarily Tired Students UVA - 12108
- [置顶] 【清单】 ---算法竞赛入门经典第二版 【例题+习题】【已搬家,不再更新..】
- 算法竞赛入门经典 第二版 习题4-8 特别困的学生 Extraordinarily Tired Students uva12108
- 算法竞赛入门经典第四章习题4-4 Cube painting UVA - 253
- 算法竞赛入门经典 第二版 习题4-1 象棋 Xiangqi uva1589
- 分数化小数 (decimal) 算法竞赛入门经典 第二版 习题 2-5
- 算法竞赛入门经典-习题3-6 纵横字谜的答案(Crossword Answers, ACM/ICPC World Finals 1994,UVa232)
- 排列 permutation 算法竞赛入门经典第二版 习题2-6
- UVa1586 分子量 算法竞赛入门经典第二版CH-3习题3-2
- 算法竞赛入门经典第六章例题6-7 Trees on the level UVA - 122
- 算法竞赛入门经典 习题3-1 得分(Score, ACM/ICPC Seoul 2005, UVa1585)
- 算法竞赛入门经典第四章习题4-9 Data Mining UVA - 1591
- 「算法竞赛入门经典 第二版」第 3 章 数组和字符串 习题解答
- 算法竞赛入门经典第四章习题4-1 Xiangqi UVA - 1589