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的更多相关文章

  1. UVa10562 Undraw the Trees

      注意点: 空树情况处理. >= && buf[r+][i-]=='-') i--; #include<cstdio> #include<cstring> ...

  2. UVA10562 数据结构题目

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

  3. [DFS遍历图]UVA10562 Undraw the Trees

    传送门: 1. UVA - 10562 2. Vjudge [看图写树]     将题目中给出的树改写为 括号表示法 即 (ROOT (SON1(...) (SON2(...)...(SONn(... ...

  4. UVA10562(看图写树,dfs)

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

  5. Uva10562——Undraw the Trees

    上来一看感觉难以下手,仔细想想就是dfs啊!!!! #include <cstdio> #include<iostream> #include<iomanip> # ...

  6. 6-17 看图写树 uva10562

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

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

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

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

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

随机推荐

  1. AGC16E Poor Turkeys

    输入样例: 10 10 8 9 2 8 4 6 4 9 7 8 2 8 1 8 3 4 3 4 2 7 输出样例#6: 5 话说这题虽然不是很OI但是确实挺锻炼思维的 一开始以为是用并查集之类的东西维 ...

  2. 构造+暴力 Codeforces Round #283 (Div. 2) B. Secret Combination

    题目传送门 /* 构造+暴力:按照题目意思,只要10次加1就变回原来的数字,暴力枚举所有数字,string大法好! */ /************************************** ...

  3. 红黑树与AVL(平衡二叉树)的区别

    关于红黑树和AVL树,来自网络: 1 好处 及 用途 红黑树 并不追求“完全平衡 ”——它只要求部分地达到平衡要求,降低了对旋转的要求,从而提高了性能. 红黑树能够以 O(log2  n)  的时间复 ...

  4. 设计模式("大话设计模式"读书笔记 C#实现)

    前言:毫无疑问 ,学习一些设计模式,对我们的编程水平的提高帮助很大.写这个博客的时候自己刚开始学习设计模式,难免有错,欢迎评论指正. 我学设计模式的第一本书是“大话设计模式”. 1.为什么要学设计模式 ...

  5. Code Kata:大整数四则运算—除法 javascript实现

    除法不可用手工算法来计算,其基本思想是反复做减法,看从被除数里面最多能减去多少个除数,商就是多少. 除法函数: 如果前者绝对值小于后者直接返回零 做减法时,不需要一个一个减,可以以除数*10^n为基数 ...

  6. 如何利用sql注入进行爆库

    SQL注入能做什么 在<SQL注入基础>一文介绍了SQL注入的基本原理和实验方法,那接下来就要问一下,SQL注入到底能什么? 估计很多朋友会这样认为:利用SQL注入最多只能获取当前表中的所 ...

  7. CSS——改变浏览器滚动条样式

    https://www.cnblogs.com/sun-rain/p/5789417.html

  8. c3p0参数详解

    <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数.Default: 3 --> <property name="acquireIncrement"& ...

  9. Python的伪造数据库:Faker

    faker 是一个可以让你生成伪造数据的Python包,在软件需求.开发.测试过程中常常需要利用一些假数据来做测试,这种时候就可以使用 Faker 来伪造数据从而用来测试. 一.Faker安装 pip ...

  10. 踩过好多次的坑 - ajax访问【mango】项目的service

    这个坑真的是踩过好多次了,好记性不如烂笔头,我总是太高估我的记忆力,这次真的是要写下来了. 项目是用的seam框架 + hibernate搭建的,架构是前辈们搭好的劳动成果,在配置service的访问 ...