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< ...
随机推荐
- 乐搏讲自动化测试-Python语言常识及前景(3)
相信小伙伴们都知道,随着软件测试行业的发展和进步自动化测试已经成为必然.在竞争日益激烈的市场环境中也是你升职加薪的利器. 所以,小编决定从今天起!将要系统.连续.高质量的持续更新「整套自动化测试」文章 ...
- [COCI2006-2007 Contest#3] BICIKLI
不难的一道题,就是码的时候出了点问题,看了其他巨佬的题解才发现问题所在... 题目大意: 给定一个有向图,n个点,m条边.请问,1号点到2号点有多少条路径?如果有无限多条,输出inf,如果有限,输出答 ...
- 递推DP HDOJ 5389 Zero Escape
题目传送门 /* 题意:把N个数分成两组,一组加起来是A,一组加起来是B,1<=A,B<=9,也可以全分到同一组.其中加是按照他给的规则加,就是一位一位加,超过一位数了再拆分成一位一位加. ...
- DFS POJ 1321 棋盘问题
题目传送门 /* DFS:因为一行或一列都只放一个,可以枚举从哪一行开始放,DFS放棋子,同一列只能有一个 */ #include <cstdio> #include <algori ...
- ACM_数数有多少(第二类Stirling数-递推dp)
数数有多少 Time Limit: 2000/1000ms (Java/Others) Problem Description: 小财最近新开了一家公司,招了n个员工,但是因为资金问题,办公楼只有m间 ...
- CSS + radius 五环
使用CSS的外链方式,写了一个五环 CSS的布局 附加radius的使用 思路: 一个大盒子里放两个子盒子: 两个子盒子上下排列,分别放3个和2个盒子用来制作圆环: 大盒子给相对定位,连个子盒子设为绝 ...
- centos服务器/dev/xvda1空间占满的解决方法
突然线上Centos的机器磁盘空间占满报警,第一反映是日志文件很大,占用了较多的磁盘空间.于是简单的上去看了一下.但是发现线上不是的地址对应的空间占的并不多.用:df -h 命令看了一下,/dev/x ...
- cocos2d-x win7 部署
1. 安装 下载python https://www.python.org/downloads/release/python-279/ 2.从官网下载cocos2d-x http://www.co ...
- vue2.0之60s验证码发送
快速的说下我的60s经历不管移动还是pc端的登录都需要发送验证信息,那么我们熟悉的那个验证按钮就不可少了.首先,我们都知道的一些基本功能.1.验证账号输入的格式正确与否(减少传递基本的错误信息)2.@ ...
- 用纯函数式思维在Java8下写的一段奇葩程序
首先说一下什么是纯函数式.在我的理解,"纯函数式"用一句话就可以描述:Anything is value.--我的理解不一定准确,但我就是这么理解的. 就是所有的东西都是值--没有 ...