UVa 10562 Undraw the Trees 看图写树
转载请注明:
仰望高端玩家的小清新 http://www.cnblogs.com/luruiyuan/
题目大意:
题目传送门:UVa 10562Undraw the Trees
给定字符拼成的树,将这些树转换成特定的括号表示的树
思路:
首先,观察样例,可以发现就是先序遍历的顺序,因此可以确定dfs
但是,还有几个地方需要考虑:
- 同一级的结点,在同一级的括号中
- 由于顺序满足先序遍历,因此不需要存储树的括号表示法,更不需要构建树,直接在遍历过程中输出即可。
- 空树:即输入为:# 时的树的处理,我不建议在此进行特判,因为本题的条件非常简单,完全可以把这个情况纳入普通情况进行处理,详见代码 27行 和 32行。
- 关于数组初始化的问题:这里数组必须初始化,我一开始为了效率不想初始化,但是马上就wa了,其原因在于(我个人推测),存在相对刁钻的数据,使得上一次的输入的tree中 ‘|’ 符号出现在了本次 tree 中某一行的 '\0' 之后,这样的话,在本次 tree 做 dfs 时,由于找到了上一次 tree 的 '|' 而导致读取到了上一次 tree 的数据。
- 森林,有可能某种情况下,可以给出多个根的树,这样就成为了一个森林,需要考虑多根情况。
- "----"中我们其实不需要显式找出其右边界,只要知道左边界即可。
- 输出数据格式的问题:这里必须吐槽UVA的测试数据,原本我的输出是最后一行不再输出换行符,没想到UVA给我判了wa,把对最后一行的特判去掉后,最后一行也输出换行,才最终ac,出题人有点不注意细节啊,必须批评一波。
- 其他 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 看图写树的更多相关文章
- 看图写树 (Undraw the Trees UVA - 10562)
题目描述: 原题:https://vjudge.net/problem/UVA-10562 题目思路: 递归找结点 //自己的代码测试过了,一直WA,贴上紫书的代码 AC代码 #include< ...
- Uva 10562 看图写树
题目链接:https://uva.onlinejudge.org/external/105/10562.pdf 紫书P170 直接在二维数组上做DFS,用的fgets函数读入数据,比较gets函数安全 ...
- UVa 10562看图写树(二叉树遍历)
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVa 10562 Undraw the Trees(递归遍历)
题目链接: https://cn.vjudge.net/problem/UVA-10562 Professor Homer has been reported missing. We suspect ...
- uva 10562 undraw the trees(烂题) ——yhx
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABB4AAAM9CAYAAAA7ObAlAAAgAElEQVR4nOyd25GsupKGywVswAV8wA ...
- UVa 10562 Undraw the Trees
题意: 将树的关系用字符串的形式给出 分析: 直接dfs搜索,第i行第j个如果是字母,判断i+1行j个是不是'|'是的话在第i+2行找第一个'-',找到后在第i+3行找字母,重复进行. 代码: #in ...
- UVA - 10562 Undraw the Trees(多叉树的dfs)
题意:将多叉树转化为括号表示法. 分析:gets读取,dfs就好了.注意,样例中一行的最后一个字母后是没有空格的. #pragma comment(linker, "/STACK:10240 ...
- UVA10562(看图写树,dfs)
这个题过的好艰难,不过真的学到好多. 关于fgets的用法真的是精髓.!isspace(c)和c!=' '是有区别的. 其它的看代码吧 #include <iostream> #inclu ...
- 6-17 看图写树 uva10562
非常好的dfs题 有很多细节 关于‘ ’ ‘0’ ’\n‘ 的处理 他们都属于isspace函数 其中 while(buf[x+2][i]=='-'&&buf[x+3][i] ...
随机推荐
- LightOJ 1321 - Sending Packets 简单最短路+期望
http://www.lightoj.com/volume_showproblem.php?problem=1321 题意:每条边都有概率无法经过,但可以重新尝试,现给出成功率,传输次数和传输时间,求 ...
- Eclipse中安装Tomcat
1. 下载Tomcat并安装: http://tomcat.apache.org/download-60.cgi 2. 下载最新Eclipse的Tomacat插件: http://www.eclips ...
- tomcat优化总结【持续更新】
配置优化 <Connector port=" maxThreads=" URIEncoding="UTF-8" maxKeepAliveRequests= ...
- Chrome 扩展开发资料
中文文档(翻译自官方文档):https://crxdoc-zh.appspot.com/apps/tut_debugging 官方英文: https://developer.chrome.com/ex ...
- Spring boot 集成Dubbox(山东数漫江湖)
前言 因为工作原因,需要在项目中集成dubbo,所以去查询dubbo相关文档,发现dubbo目前已经不更新了,所以把目光投向了dubbox,dubbox是当当网基于dubbo二次开发的一个项目,dub ...
- ThinkSnS v4后台任意文件下载漏洞
漏洞文件: /apps/admin/Lib/Action/UpgradeAction.class.php 主要问题还是出现在了180行直接将远程获取到的图片直接保存. 文中可见并没有做任何的对$dow ...
- 64_d1
DSDP-5.8-15.fc26.i686.rpm 13-Feb-2017 22:06 658926 DSDP-5.8-15.fc26.x86_64.rpm 13-Feb-2017 22:09 653 ...
- 生命周期(vue的钩子函数)
生命周期图示 创建前,创建后,挂载前,挂载后,更新前,更新后,销毁前,销毁后 beforeCreate:function(){ console.log('1-beforeCreate 组件还未被创建' ...
- MySQL-5.5.49安装、多实例、主从复制
源码安装mysql yum install ncurses-devel libaio-devel -y mkdir /server/tools -p cd /server/tools wget htt ...
- JS实现全选与取消 Jquery判断checkbox是否被选中
1.JS实现checkbox全选与取消 <body> <input type="checkbox" name="select_all"/> ...