https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1503

这道题错了好多次,一开始我直接是cin>>t,但前面可能还有空格,所以不对。就按照书上的用了fgets(buf[0],maxn,stdin)。

题目的本质就是二叉树的遍历问题,直接运用dfs遍历即可。

 #include<iostream>
#include<cstring>
#include<cctype>
using namespace std; const int maxn = ;
char buf[maxn][maxn];
int n; void dfs(int r, int i)
{
cout << buf[r][i];
cout << "(";
if (r + <n && buf[r + ][i] == '|')
{
int k = i;
while (k - >= && buf[r + ][k - ] == '-') k--;
while (buf[r + ][k] == '-' && buf[r+][k]!='\0')
{
if (!isspace(buf[r + ][k])) dfs(r + , k);
k++;
}
}
cout << ")";
}
void solve()
{
n = ;
for (;;)
{
fgets(buf[n], maxn, stdin);
if (buf[n][] == '#') break;
else n++;
}
cout << "(";
if (n)
{
int l = strlen(buf[]);
for (int i = ; i < l; i++)
if (buf[][i] != ' ') { dfs(, i); break; }
}
cout << ")"<<endl;
} int main()
{
int t;
fgets(buf[], maxn, stdin);
sscanf(buf[], "%d", &t);
while (t--) solve();
return ;
}

UVa 10562看图写树(二叉树遍历)的更多相关文章

  1. Uva 10562 看图写树

    题目链接:https://uva.onlinejudge.org/external/105/10562.pdf 紫书P170 直接在二维数组上做DFS,用的fgets函数读入数据,比较gets函数安全 ...

  2. UVa 10562 Undraw the Trees 看图写树

    转载请注明: 仰望高端玩家的小清新 http://www.cnblogs.com/luruiyuan/ 题目大意: 题目传送门:UVa 10562Undraw the Trees 给定字符拼成的树,将 ...

  3. 看图写树 (Undraw the Trees UVA - 10562)

    题目描述: 原题:https://vjudge.net/problem/UVA-10562 题目思路: 递归找结点 //自己的代码测试过了,一直WA,贴上紫书的代码 AC代码 #include< ...

  4. UVA10562(看图写树,dfs)

    这个题过的好艰难,不过真的学到好多. 关于fgets的用法真的是精髓.!isspace(c)和c!=' '是有区别的. 其它的看代码吧 #include <iostream> #inclu ...

  5. 6-17 看图写树 uva10562

    非常好的dfs题  有很多细节 关于‘ ’  ‘0’  ’\n‘  的处理  他们都属于isspace函数 其中 while(buf[x+2][i]=='-'&&buf[x+3][i] ...

  6. 看图写代码---看图写代码 阅读<<Audio/Video Connectivity Solutions for Virtex-II Pro and Virtex-4 FPGAs >>

    看图写代码 阅读<<Audio/Video Connectivity Solutions for Virtex-II Pro and Virtex-4 FPGAs >> 1.S ...

  7. UVa 10562 Undraw the Trees(递归遍历)

    题目链接: https://cn.vjudge.net/problem/UVA-10562 Professor Homer has been reported missing. We suspect ...

  8. POJ 题目1145/UVA题目112 Tree Summing(二叉树遍历)

    Tree Summing Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8132   Accepted: 1949 Desc ...

  9. 算法随笔-二叉树遍历的N种姿势

    最近在练习用Python刷算法,leetcode上刷了快300题.一开始怀疑自己根本不会写代码,现在觉得会写一点点了,痛苦又充实的刷题历程.对我这种半路出家的人而言,收获真的很大. 今天就从二叉树遍历 ...

随机推荐

  1. window.location.href 失效的解决办法

    第一种:在window.location.href 后面加上 window.event.returnValue = false; 如: <a href="#" onclick ...

  2. C语言语法之运算符及优先级

    注:该内容整理自以下链接. http://blog.csdn.net/huangblog/article/details/8271791 表:C语言运算符及优先级 优先级 运算符 名称或含义 使用形式 ...

  3. iOS中JS 与OC的交互(JavaScriptCore.framework)

    iOS中实现js与oc的交互,目前网上也有不少流行的开源解决方案: 如:react native 当然一些轻量级的任务使用系统提供的UIWebView 以及JavaScriptCore.framewo ...

  4. Hbase WAL线程模型源码分析

    版权声明:本文由熊训德原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/257 来源:腾云阁 https://www.qclo ...

  5. MySQL SQL

    SQL语句错误: Column count doesn't match value count at row 1 列计数与第1行的值计数不匹配 You have an error in your SQ ...

  6. C#日常知识

    常量: 定义常量:const; 条件运算符: 表达式1?表达式2:表达式3[如果正确则执行表达式2,不正确执行表达式3] (例如:int result; result = 5>4?100:200 ...

  7. ITPUB网站的知识索引汇总

    1. ITPUB知识索引树 http://www.itpub.net/tree/ http://www.itpub.net/pubtree/index.htm 2. ITPUB知识索引贴——全文索引 ...

  8. jQuery对input select操作小结

    //遍历option和添加.移除optionfunction changeShipMethod(shipping){ var len = $("select[@name=ISHIPTYPE] ...

  9. iOS开发UI篇—Quartz2D(自定义UIImageView控件)

    iOS开发UI篇—Quartz2D(自定义UIImageView控件) 一.实现思路 Quartz2D最大的用途在于自定义View(自定义UI控件),当系统的View不能满足我们使用需求的时候,自定义 ...

  10. Charles的使用

    简介 Charles是在Mac下常用的截取网络封包的工具,在做iOS开发时,我们为了调试与服务器端的网络通讯协议,常常需要截取网络封包来分析.Charles通过将自己设置成系统的网络访问代理服务器,使 ...