习题6-2 S-Trees(树)
来源:互联网 发布:全钟穆勒编程软件下载 编辑:程序博客网 时间:2024/05/01 09:14
题意:
看白书
要点:
题目很简单,就是树的水题,我是直接建树然后模拟,第一次WA主要是最后想输出字符串但没有在末尾加上\0,编译器可以通过但会WA,下次要注意了。这题也可以不建树直接用下标法做,我看了一下网上的代码,确实简单多了
1.建树+模拟
#include<stdio.h>#include<string.h>#include<stdlib.h>#include<math.h>int tree[10000],rule[10];char leaf[10000],out[10];int n;void search(){int num,i,j,k;char s[10];scanf("%d", &num);for (i = 0; i < num; i++){scanf("%s", s);for (j = 0; j < strlen(s); j++){int x = rule[j+1];for (k = pow(2, x-1); k <= pow(2, x) - 1; k++)tree[k] = s[j] - 48;}int ans=1;for (j = 1; j <= n; j++){if (tree[ans] == 0)ans = 2 * ans;else if (tree[ans] == 1)ans = 2 * ans + 1;}ans = ans - pow(2, n);out[i] = leaf[ans];}out[num] = '\0';//这里非常重要,第一次WA在这里}int main(){int count = 1;while (~scanf("%d", &n), n){memset(tree, 0, sizeof(tree));memset(rule, 0, sizeof(rule));int i,j;char layer[10];for (i = 1; i <= n; i++){scanf("%s", layer);rule[layer[1] - 48] = i;//用rule存储真正的x的顺序}scanf("%s", leaf);search();printf("S-Tree #%d:\n", count++);printf("%s\n", out);printf("\n");}return 0;}
#include<stdio.h>#include<string.h>#include<stdlib.h>#define maxn 15000int main(){int n,i,j,t=1;while (~scanf("%d", &n), n){char leaf[maxn], xn[5], out[maxn];int layer[maxn];for (i = 1; i <= n; i++){scanf("%s", xn);layer[i] = xn[1] - '0';}int num;char temp[maxn];scanf("%s%d", leaf, &num);for (i = 0; i < num; i++){scanf("%s", temp);int k = 0; //这里开始为0后面直接就等于leaf对应下标for (j = 1; j <= n; j++){k = temp[layer[j]-1] == '1' ? (k << 1) + 1 : k << 1;//下标都是对应的,直接找就可以}out[i] = leaf[k];}out[num] = '\0';printf("S-Tree #%d:\n%s\n\n", t++, out);}return 0;}
0 0
- 习题6-2 S-Trees(树)
- UVa712 习题6-2 S树(S-Trees)
- 习题6-2 S树(S-Trees, UVa 712)
- 习题6-2 UVA 712 S-Trees S树
- 习题6-2 S-Trees UVA
- 算法竞赛入门经典 第二版 习题6-2 S树 S-Trees uva712
- 算法入门经典习题6-2,uva712,S-Trees
- Uva712 S-Trees 【二分思想】【习题6-2】
- S-Trees(二叉树)
- [UVa 712] S 树(S-Trees)
- 习题6-2 S树 UVa712
- 习题6-2 S树 UVa 712
- S-Trees UVA712(二叉树)
- UVa 712 S-Trees S树
- S树(S-Trees, Uva 712)
- uva 712 S-Trees(利用二叉树的特点)
- POJ 题目1105 S-Trees(二叉树模拟)
- zoj S-Trees 1150 (二叉树模拟)
- 电脑公司最新稳定win7系统下载
- Opencv图像偏色检测
- kcatSniM.155
- Linux系统学习入门(1)(转载)
- 关于RCP产品无法进行远程调试的说明(源码断点和高亮显示位置不对)
- 习题6-2 S-Trees(树)
- 重构(改善既有代码的设计)-- 读书笔记1
- 通过js框架jquery判断IE浏览器版本来解决浏览器兼容性问题
- Linux中查询I/O地址范围
- Axis2开发WebService客户端 的3种方式(转载)
- java进制间转换
- mysql数据库存储路径更改 数据文件位置
- 今日BBC
- java模糊搜索