Uva10562
Professor Homer has been reported missing. We suspect that his recent research works might have had something to with this. But we really don't know much about what he was working on! The detectives tried to hack into his computer, but after hours of failed efforts they realized that the professor had been lot more intelligent than them. If only they could realize that the professor must have been absent minded they could get the clue rightaway. We at the crime lab were not at all surprised when the professor's works were found in a 3.5" floppy disk left inside the drive.
The disk contained only one text file in which the professor had drawn many trees with ASCII characters. Before we can proceed to the next level of investigation we would like to match the trees drawn with the ones that we have in our database. Now you are the computer geek -- we leave this trivial task for you. Convert professor's trees to our trees.
Professor's Trees
The first line of the input file (which you can assume comes from standard input) contains the number of trees, T (1 <= T <= 20) drawn in the file. Then you would have T trees, each ending with a single hash ('#') mark at the beginning of the line. All the trees drawn here are drawn vertically in top down fashion. The labels of each of node can be any printable character except for the symbols '-', '|', ' ' (space) and '#'. Every node that has children has a '|'symbol drawn just below itself. And in the next line there would be a series of '-' marks at least as long as to cover all the immediate children. The sample input section will hopefully clarify your doubts if there is any. No tree drawn here requires more than 200 lines, and none of them has more than 200 characters in one line.
Our Trees
Our trees are drawn with parenthesis and the node labels. Every subtree starts with an opening parenthesis and ends with a closing parenthesis; inside the parenthesis the node labels are listed. The sub trees are always listed from left to right. In our database each tree is written as a single string in one line, and they do not contain any character except for the node labels and the parenthesis pair. The node labels can be repeated if the original tree had such repetitions.
Sample Professor’s Trees Corresponding Our Trees
|
2 A | -------- B C D | | ----- - E F G # e | ---- f g #
|
(A(B()C(E()F())D(G()))) (e(f()g()))
|
分析:这道题其实并不是很难,细节处理比较麻烦。
首先可以发现这道题的输入时以“递归”形式给出的,那么我们也用递归来做,如果当前节点有子节点就往下递归,剩下的就是字符串处理的一些细节了.
当时在做这道题的时候,nl和nr全都初始化为i,事实上这样是不对的,因为如果找不到一个不等于-的字符,那么接下来的区间大小就会为1,答案是错误的,以后要细心了.
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <string> using namespace std; int T, top, cnt;
char s[][], ans[]; bool islable(char c)
{
if (c == ' ' || c == '|' || c == '#' || c == '-' || c == '/n')
return false;
return true;
} void dfs(int l, int r, int depth)
{
ans[++cnt] = '(';
for (int i = l; i <= r && i < strlen(s[depth]); i++)
if (islable(s[depth][i]))
{
if (s[depth + ][i] == '|')
{
ans[++cnt] = s[depth][i];
int nl = , nr = strlen(s[depth + ]) - , j = i;
for (int j = i; j >= ; j--)
if (s[depth + ][j] != '-')
{
nl = j;
break;
}
for (int j = i; j <= strlen(s[depth + ]) - ; j++)
if (s[depth + ][j] != '-')
{
nr = j;
break;
}
dfs(nl, nr, depth + );
}
else
{
ans[++cnt] = s[depth][i];
ans[++cnt] = '(';
ans[++cnt] = ')';
}
}
ans[++cnt] = ')';
} int main()
{
cin >> T;
getchar(); //过滤掉回车符
while (T--)
{
top = ;
cnt = ;
memset(ans, , sizeof(ans));
memset(s, , sizeof(s));
while ()
{
gets(s[++top]);
if (s[top][] == '#')
break;
}
dfs(, strlen(s[]) - , );
for (int i = ; i <= cnt; i++)
printf("%c", ans[i]);
printf("\n");
} return ;
}
Uva10562的更多相关文章
- UVa10562 Undraw the Trees
注意点: 空树情况处理. >= && buf[r+][i-]=='-') i--; #include<cstdio> #include<cstring> ...
- UVA10562 数据结构题目
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- [DFS遍历图]UVA10562 Undraw the Trees
传送门: 1. UVA - 10562 2. Vjudge [看图写树] 将题目中给出的树改写为 括号表示法 即 (ROOT (SON1(...) (SON2(...)...(SONn(... ...
- UVA10562(看图写树,dfs)
这个题过的好艰难,不过真的学到好多. 关于fgets的用法真的是精髓.!isspace(c)和c!=' '是有区别的. 其它的看代码吧 #include <iostream> #inclu ...
- Uva10562——Undraw the Trees
上来一看感觉难以下手,仔细想想就是dfs啊!!!! #include <cstdio> #include<iostream> #include<iomanip> # ...
- 6-17 看图写树 uva10562
非常好的dfs题 有很多细节 关于‘ ’ ‘0’ ’\n‘ 的处理 他们都属于isspace函数 其中 while(buf[x+2][i]=='-'&&buf[x+3][i] ...
- UVa 10562 Undraw the Trees(递归遍历)
题目链接: https://cn.vjudge.net/problem/UVA-10562 Professor Homer has been reported missing. We suspect ...
- 看图写树 (Undraw the Trees UVA - 10562)
题目描述: 原题:https://vjudge.net/problem/UVA-10562 题目思路: 递归找结点 //自己的代码测试过了,一直WA,贴上紫书的代码 AC代码 #include< ...
随机推荐
- golang——strconv包常用函数
1.func ParseBool(str string) (value bool, err error) 返回字符串表示的bool值.它接受1.0.t.f.T.F.true.false.True.Fa ...
- Agar.io 简单但是有趣的网页游戏
攻略,进阶 上榜第一次 (有点水,九百多分) 上榜第二次 (完成四杀,逆袭上榜) 上榜第三次 (忘写名字,自己补上) 上榜第四次 (人生巅峰!) 上榜第五次 (踩了狗屎运,上榜这么容易了?收了一个小 ...
- Spring MVC过滤器-HiddenHttpMethodFilter
参考来源:http://blog.csdn.net/geloin/article/details/7444321 浏览器form表单只支持GET与POST请求,而DELETE.PUT等method并不 ...
- mysqladmin(MySQL管理工具)
mysqladmin是一个执行管理操作的客户端程序.它可以用来检查服务器的配置和当前状态.创建和删除数据库等. 1.mysqladmin命令的语法: shell > mysqladmin [op ...
- .Net MVC 前台验证跟后台验证
前台验证: 首先你得有一个参数类,参数类代码如下 验证标记总结 [DisplayName("邮箱:")] [Required(ErrorMessage = " ...
- Debug技巧(1)
首先声明,以下有些是自己遇到的问题自己解决了,其它里面包括了网上看到的Debug经验和书里看到的经验,时间问题就不一一说明,如有侵权,私信我进行删除,我会道歉.我写这个的本意是记录我学习中遇到的问题, ...
- ionic3带参数返回原来页面
最近用ionic3+angular4做项目.我遇到了个问题,我返回原来页面时一般都会调用this.navCtrl.pop()方法,但这个方法不能携带参数.怎么办? 可以写个回调方法. 我在a页面定义个 ...
- git reset作用
git reset: 1. 文件从暂存区回退到工作区,撤销add 2. 版本回退 一:文件从暂存区回退到工作区,撤销add 如果想取消某个add的文件,可以使用该命令来进行撤销操作 撤消add:gi ...
- 【C++】智能指针简述(二):auto_ptr
首先,我要声明auto_ptr是一个坑!auto_ptr是一个坑!auto_ptr是一个坑!重要的事情说三遍!!! 通过上文,我们知道智能指针通过对象管理指针,在构造对象时完成资源的分配及初始化,在析 ...
- Sturts2几个常用内建拦截器的介绍
Sturts2几个常用内建拦截器的介绍:1)conversation:这是一个处理类型转换错误的拦截器,它负责将类型转换错误从ActionContext中取出,并转换成Action的FieldErro ...