博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
uva 10562 - Undraw the Trees
阅读量:5104 次
发布时间:2019-06-13

本文共 3712 字,大约阅读时间需要 12 分钟。

Problem D

Input: Standard Input

Output: Standard Output

Time Limit: 2 Seconds

 

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()))

 

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;#define ms(arr, val) memset(arr, val, sizeof(arr))#define mc(dest, src) memcpy(dest, src, sizeof(src))#define N 205#define INF 0x3fffffff#define vint vector
#define setint set
#define mint map
#define lint list
#define sch stack
#define qch queue
#define sint stack
#define qint queue
char s[N][N];int t, n, pos[N];void dfs(int i){ //putchar('('); int j = i + 1; bool tag = false; for (; s[j][pos[j]]; pos[j]++) { if (s[i][pos[j]] == '-' && s[j][pos[j]] != '#' && s[j][pos[j]] != ' ' && s[j][pos[j]] != '-' && s[j][pos[j]] != '|') { tag = true; putchar(s[j][pos[j]]); putchar('('); if (s[j + 1][pos[j]] == '|') { dfs(i + 3); } putchar(')'); } else if (tag && s[i][pos[j]] != '-')//特别注意 { tag = false; return; } } //putchar(')');}int main(){ ms(s[0], '-'); scanf("%d", &t); getchar(); while (t--) { n = 1; while (gets(s[n]), s[n][0] != '#') n++; ms(s[n], 0); ms(pos, 0); putchar('('); dfs(0); putchar(')'); putchar('\n'); } return 0;}

 

转载于:https://www.cnblogs.com/jecyhw/p/3914352.html

你可能感兴趣的文章
getElement的几中属性介绍
查看>>
第二阶段冲刺-01
查看>>
BZOJ1045 HAOI2008 糖果传递
查看>>
JavaScript 克隆数组
查看>>
【题解】[P4178 Tree]
查看>>
cer证书签名验证
查看>>
【深度学习】caffe 中的一些参数介绍
查看>>
QML学习笔记之一
查看>>
App右上角数字
查看>>
小算法
查看>>
WPF中实现多选ComboBox控件
查看>>
TestNG入门
查看>>
【ul开发攻略】HTML5/CSS3菜单代码 阴影+发光+圆角
查看>>
IO—》Properties类&序列化流与反序列化流
查看>>
Codeforces 719B Anatoly and Cockroaches
查看>>
ActiveMQ与spring整合
查看>>
格式化输出数字和时间
查看>>
关于TFS2010使用常见问题
查看>>
URL编码与解码
查看>>
剑指offer系列6:数值的整数次方
查看>>