博客
关于我
例题6-17 看图写树(Undraw the Trees, UVa 10562)
阅读量:285 次
发布时间: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/

你可能感兴趣的文章
java —— this 关键字
查看>>
java —— static 关键字
查看>>
Firefox 69 已可在 Fedora 中获取 | Linux 中国
查看>>
在 Python 调试过程中设置不中断的断点 | Linux 中国
查看>>
AI 系统向自动化编码迈进 | Linux 中国
查看>>
使用 Jupyter Notebooks 构建一个远程管理控制台 | Linux 中国
查看>>
使用开源可视化工具来理解你的 Python 代码 | Linux 中国
查看>>
【2021 ECUG Con】聚势而来,与你相约花开时
查看>>
硬核观察 | 有人在比特币骗局中损失了 10 个比特币
查看>>
FreeDOS 的简单介绍 | Linux 中国
查看>>
使用 top 命令了解 Fedora 的内存使用情况 | Linux 中国
查看>>
怎样解决 “sub process usr bin dpkg returned an error code 1” 错误
查看>>
Bat:一种具有语法高亮和 Git 集成的 Cat 类命令 | Linux 中国
查看>>
Linux 上最好的五款音乐播放器 | Linux 中国
查看>>
网易云首倡中台方法论,发布全链路中台技术方案
查看>>
传输层协议
查看>>
如何加载dll文件计算UDS服务的秘钥
查看>>
细数哪些网络用户需要换IP?
查看>>
codeforces1307D 1900分最短路
查看>>
2020牛客暑期多校训练营(第九场)
查看>>