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< ...
随机推荐
- SQL 事务篇和约束
数据库事务: 数据库事务(Database Transaction) ,是指作为单个逻辑工作单元执行的一系列操作,要么完全地执行,要么完全地不执行 事务是恢复和并发控制的基本单位.事务应该具有4个属性 ...
- Asp.NET 知识点总结(一)
1.简述 private. protected. public. internal 修饰符的访问权限. 答 . private : 私有类,私有成员, 在类的内部才可以访问. protected : ...
- 93. [NOIP2001] 数的划分
问题描述 将整数n分成k份,且每份不能为空,任意两种方案不能相同(不考虑顺序). 例如:n=7,k=3,下面三种分法被认为是相同的. 1,1,5; 1,5,1; 5,1,1; 问有多少种不同的分法. ...
- C语言调用Python
python模块:demo.py def print_arg(str): print str def add(a,b): print 'a=', a print 'b=', b return a + ...
- oracle 手动配置服务器端和客户端
1.oracle 服务器端配置 将oracle安装完成之后,在Net Configuration Assistant配置 1.监听程序配置 先找到Net Configuration Assistant ...
- 使用Spring框架的步骤
“好记性,不如烂笔头”.今天正式接触了Spring框架,第一次接触Spring框架感觉Spring框架简化了好多程序代码,开发效率大大提高.现在介绍使用Spring框架的步骤.(使用spring-fr ...
- logging,numpy,pandas,matplotlib模块
logging模块 日志总共分为以下五个级别,这五个级别自下而上进行匹配debug->info->warning->error->critical,默认的最低级别warning ...
- os下,vs code 自动编译ts
nodejs安装ts npm install -g typescript 进入工程目录,用命令初始化ts(生成tsconfig.json) tsc --init 如果要指定生成目录,打开tsconfi ...
- Django线上部署教程:腾讯云+Ubuntu+Django+Uwsgi(转载)
网站名称: 向东的笔记本 本文链接: https://www.eastnotes.com/post/29 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议.转载请注明出处! ...
- 用Docker实现nginx多端口
一.安装docker 需要阿里的epel源,需要联网 [root@bogon ~]#yum -y install docker [root@bogon ~]#systemctl start docke ...