UVa 10562 Undraw the Trees (二叉树先序遍历)

来源:互联网 发布:水洗唛打印软件 编辑:程序博客网 时间:2024/06/09 19:26

10562 - Undraw the Trees

Time limit: 3.000 seconds 

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=104&page=show_problem&problem=1503

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 withASCII 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 haveT 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 than200 lines, and none of them has more than200 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()))                                                              


dfs这张图,关键是怎么找子树——以'|'为中心向左右扩展,遇到非'-'就停止,这样得到的left和right就是子树的范围。


完整代码:

/*0.025s*/#include<cstdio>#include<cstring>char tree[300][300];void dfs(int l, int r, int cur){int i, ll, rr;putchar('(');for (i = l; i < r && tree[cur][i]; ++i)if (tree[cur][i] != '#' && tree[cur][i] != ' ')///'#'为终点{putchar(tree[cur][i]);if (tree[cur + 1][i] == '|')///下方有节点{for (ll = i; ll > 0 && tree[cur + 2][ll] == '-'; --ll);for (rr = i; tree[cur + 2][rr] == '-'; ++rr)///寻找下面子树的范围("----"这样);dfs(ll, rr, cur + 3);}elseprintf("()");}putchar(')');}int main(){int T, n;scanf("%d\n", &T);while (T--){memset(tree, 0, sizeof(tree));n = 0;while (gets(tree[n]), tree[n++][0] != '#')///读取图像;dfs(0, strlen(tree[0]), 0);putchar(10);}return 0;}

原创粉丝点击