博客
关于我
例题6-17 看图写树(Undraw the Trees, UVa 10562)
阅读量:292 次
发布时间:2019-03-03

本文共 921 字,大约阅读时间需要 3 分钟。

原题链接:

分类:树
备注:多叉树的DFS

因为最外面那个括号让我搞了半天…看一眼紫书,原来最外层的括号和里面的括号规律不同,直接放在外面输出就好了…

代码如下:

#include
#include
#include
using namespace std;string line[205];int depth;void dfs(int L,int R,int pos) { for (int i = L; i < R && i < line[pos].length(); i++) { if (line[pos][i] != ' ' && line[pos][i] != '-' && line[pos][i] != '#') { printf("%c(", line[pos][i]); if (pos + 1 < depth && i < line[pos + 1].length() && line[pos + 1][i] == '|') { int l, r; l = r = i; while (l > 0 && line[pos + 2][l - 1] != ' ')l--; while (line[pos + 2][r] != ' ' && r < line[pos + 2].length())r++; dfs(l, r, pos + 3); } printf(")"); } }}int main(void) { int T; scanf("%d", &T); getchar(); while (T--) { depth = 0; while (getline(cin, line[depth++])) { if (line[depth - 1] == "#")break; } depth--; printf("("); if (depth)dfs(0, line[0].length(), 0); printf(")\n"); } return 0;}

转载地址:http://gcel.baihongyu.com/

你可能感兴趣的文章
mysql 通过查看mysql 配置参数、状态来优化你的mysql
查看>>
mysql 里对root及普通用户赋权及更改密码的一些命令
查看>>
Mysql 重置自增列的开始序号
查看>>
mysql 锁机制 mvcc_Mysql性能优化-事务、锁和MVCC
查看>>
MySQL 错误
查看>>
mysql 随机数 rand使用
查看>>
MySQL 面试题汇总
查看>>
MySQL 面试,必须掌握的 8 大核心点
查看>>
MySQL 高可用性之keepalived+mysql双主
查看>>
mysql 默认事务隔离级别下锁分析
查看>>
Mysql--逻辑架构
查看>>
MySql-2019-4-21-复习
查看>>
mysql-5.7.18安装
查看>>
MySQL-Buffer的应用
查看>>
mysql-cluster 安装篇(1)---简介
查看>>
mysql-connector-java各种版本下载地址
查看>>
mysql-EXPLAIN
查看>>
mysql-group_concat
查看>>
MySQL-redo日志
查看>>
MySQL-【1】配置
查看>>