转载请注明:

仰望高端玩家的小清新 http://www.cnblogs.com/luruiyuan/

题目大意:

题目传送门:UVa 10562Undraw the Trees

给定字符拼成的树,将这些树转换成特定的括号表示的树

思路:

首先,观察样例,可以发现就是先序遍历的顺序,因此可以确定dfs

但是,还有几个地方需要考虑:

  1. 同一级的结点,在同一级的括号中
  2. 由于顺序满足先序遍历,因此不需要存储树的括号表示法,更不需要构建树,直接在遍历过程中输出即可。
  3. 空树:即输入为:# 时的树的处理,我不建议在此进行特判,因为本题的条件非常简单,完全可以把这个情况纳入普通情况进行处理,详见代码 27行 和 32行。
  4. 关于数组初始化的问题:这里数组必须初始化,我一开始为了效率不想初始化,但是马上就wa了,其原因在于(我个人推测),存在相对刁钻的数据,使得上一次的输入的tree中 ‘|’ 符号出现在了本次 tree 中某一行的 '\0' 之后,这样的话,在本次 tree 做 dfs 时,由于找到了上一次 tree 的 '|' 而导致读取到了上一次 tree 的数据。
  5. 森林,有可能某种情况下,可以给出多个根的树,这样就成为了一个森林,需要考虑多根情况。
  6. "----"中我们其实不需要显式找出其右边界,只要知道左边界即可。
  7. 输出数据格式的问题:这里必须吐槽UVA的测试数据,原本我的输出是最后一行不再输出换行符,没想到UVA给我判了wa,把对最后一行的特判去掉后,最后一行也输出换行,才最终ac,出题人有点不注意细节啊,必须批评一波。
  8. 其他 trick:比如用 valid_node 函数中,用字符串匹配代替 if else;读取 tree 时通过 while 循环一行完成读取,而不必写成函数徒增复杂度;使用 fgets 和 sscanf 来读取含有多余空白符的数字。
 #include<cstdio>
#include<cstdlib>
#include<cstring> const int SIZE = + ;
char tree[SIZE][SIZE]; int num = ; // tree num
int cur = ; // cur num of row in tree bool valid_node(char ch);
void dfs_tree(int row, int col); int main(){
// use fgets() and sscanf() to read white char and then remove them.
fgets(tree[], SIZE, stdin);
sscanf(tree[], "%d", &num);
while(num--){
// read tree
cur = ;
// memset is compulsory because the '|' behind the '\0' of the last tree will
// have a bad influence on current tree
memset(tree, , sizeof(tree));
// use fgets() instead of gets() to read tree in order to avoid overflow
while(fgets(tree[cur], SIZE, stdin) != NULL && tree[cur][] != '#')cur++;
// dfs - for the condition of forest
printf("(");
for(int i=; tree[][i]; i++)
if(valid_node(tree[][i]))
dfs_tree(, i);
// postprocess. If we use "if(num > 0)puts("");", it will cause "wrong answer"
printf(")\n");
}
} bool valid_node(char ch){
return strchr("-| #\n", ch) == NULL; // we need add '\n' because fegets() will read the '\n' at end of each line to the tree.
} void dfs_tree(int row, int col){
printf("%c(", tree[row][col]); // print root of the tree, and then print '('
if(row < cur && tree[++row][col] == '|'){
int i=col;
row++;
while(i - >= && tree[row][i-] == '-')i--; // left bouder of "---"
for(;tree[row][i] == '-'; i++)
if(valid_node(tree[row+][i])) dfs_tree(row+, i);
}
printf(")");
}

啦啦啦,今天就到这里啦~改天见*★,°*:.☆( ̄▽ ̄)/$:*.°★* 。

UVa 10562 Undraw the Trees 看图写树的更多相关文章

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

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

  2. Uva 10562 看图写树

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

  3. UVa 10562看图写树(二叉树遍历)

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

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

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

  5. uva 10562 undraw the trees(烂题) ——yhx

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABB4AAAM9CAYAAAA7ObAlAAAgAElEQVR4nOyd25GsupKGywVswAV8wA ...

  6. UVa 10562 Undraw the Trees

    题意: 将树的关系用字符串的形式给出 分析: 直接dfs搜索,第i行第j个如果是字母,判断i+1行j个是不是'|'是的话在第i+2行找第一个'-',找到后在第i+3行找字母,重复进行. 代码: #in ...

  7. UVA - 10562 Undraw the Trees(多叉树的dfs)

    题意:将多叉树转化为括号表示法. 分析:gets读取,dfs就好了.注意,样例中一行的最后一个字母后是没有空格的. #pragma comment(linker, "/STACK:10240 ...

  8. UVA10562(看图写树,dfs)

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

  9. 6-17 看图写树 uva10562

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

随机推荐

  1. vue-route-transition路由前进后退动画

    vue-route-transition vue router 切换动画 特性 模拟前进后退动画 基于css3流畅动画 基于sessionStorage,页面刷新不影响路由记录 路由懒加载,返回可记录 ...

  2. 【BZOJ】1607: [Usaco2008 Dec]Patting Heads 轻拍牛头

    [算法]模拟 #include<cstdio> #include<algorithm> using namespace std; ,maxm=; int a[maxn],A[m ...

  3. 【游记】CTSC&APIO2017

    GDOI回来不到两天就前往北京参加CTSC和APIO. CTSC Day1 [考试] T1一道神奇的题,很快想到O(n2)做法,感觉ctsc题目难度应该很大,就没马上想着出正解(事实上这届CTSC偏水 ...

  4. MySQL 基于 GTID 主从架构添加新 Slave 的过程

    内容全部来自: How to create/restore a slave using GTID replication in MySQL 5.6 需求说明 需求: 对于已经存在的 MySQL 主从架 ...

  5. Drainage Ditches(POJ1273+网络流+Dinic+EK)

    题目链接:poj.org/problem?id=1273 题目: 题意:求最大流. 思路:测板子题,分别用Dinic和EK实现(我的板子跑得时间均为0ms). Dinic代码实现如下: #includ ...

  6. springmvc4处理get和post请求中文乱码问题

    1.在springmvc4处理get和post请求的问题 参看大牛博客连接:https://blog.csdn.net/qq_41665356/article/details/80234392

  7. chrome表单自动填充导致input文本框背景变成偏黄色问题

    你曾遇到过吗? 困扰宝宝好久的问题,本以为是什么插件导致的,结果是chrome浏览器自动填充文本时默认的样式,搜嘎. 一.修改自动填充input文本框背景色: 使用以下代码 可以设置自己的想要的默认文 ...

  8. 移动端 H5 页面注意事项

    1. 单个页面内容不能过多 设计常用尺寸:750 x 1334 / 640 x 1134,包含了手机顶部信号栏的高度. 移动端H5活动页面常常需要能够分享到各种社交App中,常用的有 微信.QQ 等. ...

  9. this指针再解

     this.new.call和apply的相关问题 讲解this指针的原理是个很复杂的问题,如果我们从javascript里this的实现机制来说明this,很多朋友可能会越来越糊涂,因此本篇打算换一 ...

  10. KVM虚拟机建立快照

    部分转载: http://blog.csdn.net/gg296231363/article/details/6899533 windows虚拟机默认镜像格式为raw,快照默认格式为qcow2.win ...