UVa 10562 - Undraw the Trees解题报告
来源:互联网 发布:新疆税友软件 编辑:程序博客网 时间:2024/04/29 10:04
题意:根据图形化的树,输出前序遍历的节点。
思路:一开始我用边读边建的方法遍历,后来发现由于输入的两棵子树数据都在同一行,行不通。这道题的关键就在于处理"--------"区间决定的子树。在网上看了一下,有人想到了根据‘————“区间进行扫描,因为每一层的节点都有上面一层的”——",根节点没有,但是我们可以自己构造。这一点很巧妙,我没有想到。
于是,根据上面的方法,我们用二维数组来存储整个树的原始数据,再初始化第一层上面的“——’区间。然后递归求解。
//10562 - Undraw the Trees#include <iostream>#include <cstring>using namespace std;char tree[210][210];int high;void DFS(int x, int y);int main(){//freopen("data.txt", "r", stdin);int cases;scanf("%d", &cases);getchar();while (cases--){high = 1;while (cin.getline(tree[high], 210) && strcmp(tree[high], "#") != 0)high++;if(high == 1)//处理空树的情况printf("()\n");else{int len = strlen(tree[1]);for(int i = 0; i < len; i++)//初始化第一层的'-'tree[0][i] = '-';printf("(");DFS(0, 0);printf(")\n");memset(tree, 0, sizeof(tree));}}return 0;}void DFS(int x, int y){for(int i = y; tree[x][i] == '-'; i++)//根据节点上面的'-'来扫描if(tree[x + 1][i] != ' ' && tree[x + 1][i] != '\0')//输出可打印字符{printf("%c(", tree[x + 1][i]);if(x + 1 < high && tree[x + 2][i] == '|')//判断是否有子树{int j;for(j = i; j && tree[x + 3][j - 1] == '-'; j--);//确定区间起点DFS(x + 3, j);}printf(")");}}
0 0
- UVa 10562 - Undraw the Trees解题报告
- uva 10562 - Undraw the Trees
- uva 10562 - Undraw the Trees
- uva 10562 - Undraw the Trees
- UVa 10562 - Undraw the Trees
- UVA 10562 - Undraw the Trees
- UVA 10562 Undraw the Trees
- uva 10562 - Undraw the Trees
- uva 10562 - Undraw the Trees
- UVa 10562 - Undraw the Trees
- UVA 10562 Undraw the Trees
- UVA - 10562 Undraw the Trees
- uva-10562Undraw the Trees
- UVA 10562 - Undraw the Trees
- UVA - 10562 Undraw the Trees
- UVa 10562 - Undraw the Trees
- Uva - 10562 - Undraw the Trees
- uva 10562 Undraw the Trees
- linux setsockopt函数的用法
- 求奇数的乘积
- 视图能否被更新
- 求职失败者的5种特征
- 八种基本数据类型占用字节空间总结
- UVa 10562 - Undraw the Trees解题报告
- TP-LINK-WR703N & Openwrt
- 软件决定硬件的发挥,硬件决定软件的结果。
- Linux的控制台(TTY/PTY)与多任务(MULTI-TASKING
- 声明变量属性时 ?号的作用
- 数组之杨辉三角
- php算法
- 返回上一页并自动刷新的方法JavaScript
- rhel升级内核过程