编程介的小学生 2017-03-27 05:41 采纳率: 20.5%
浏览 796
已采纳

Tree Format Converter

In computer science, a tree is a widely-used data structure, and there're a lot of problems dealing with trees in programming contests. However, tree is not a data structure like array which can be represented easily in the input text (just a sequence of elements with its size or a terminator). Instead, it is a graphic structure and can be represented by text in many different ways. Each problem with trees just choose one of them as input format, thus result in some problems: if you've solved a problem with a specific input format, and meet another problem which requires the same algorithm but uses a different input format, maybe you have to write a completely new program to work with the new format. That's obviously a waste of time. Your task here is just write a tree format converter that can convert a tree format into another. For simplicity, we just deal with rooted trees here, and use terminator (-1) instead of size for some formats since other formats do not require such information and thus there's no need to record the size of the tree. There're six formats that your program should handle here:

Children list (CL): There's a line for the root and each internal node, each begins with the node number and the size of its children, then a list of its children's numbers followed. All those lines are listed in preorder of the node they specify. A line of -1 terminates a tree.

Parent sequence (PS): There's a line for each node with two integers: the node number and its parent's number. All nodes are listed in preorder, the parent's number of the root is -1 since it has no parent. A line of -1 terminates a tree.

Preorder and postorder sequences (PP): Just two lines of node numbers terminated by -1. The node numbers in the first line are listed in preorder, and those in the second line are listed in postorder.

Preorder and depth sequence (PD): There's a line for each node with two integers: the node number and its depth. All nodes are listed in preorder. A line of -1 terminates a tree.

Parentheses language (PL): The grammar of this language is:

T ::= "(" N S ")"
S ::= " " T S
| empty
N ::= node number
That is, trees have parentheses around them, and node numbers of the roots, followed by arbitrarily many (maybe none) subtrees separated by single space character. For example: (1 (2 (4) (5)) (3 (6)))

Parentheses and commas (PC): A tree is begin with the node number of its root, and all its subtrees (if any) are listed inside a pair of parentheses and separated by commas. There's a space before each left parenthesis and after each comma. For example: 1 (2 (4, 5), 3 (6))

Input

Each test case begins with a line with the format "XX -> YY", where XX is the two-letter abbreviation of the source format, and YY is the abbreviation of the target name. Then followed a description of a non-empty tree in the source format. All node numbers are distinct non-negative integers less than 32768.

There's a blank line between every two test cases, processing to the end of file.

Output

For each test case, you should print the tree in the target format. And print a blank line between every two test cases.

Sample Input

CL -> PS
1 2 2 3
2 2 4 5
3 1 6
-1

PP -> PD
1 2 4 5 3 6 -1
4 5 2 6 3 1 -1

PL -> PC
(1 (2 (4) (5)) (3 (6)))
Sample Output

1 -1
2 1
4 2
5 2
3 1
6 3
-1

1 0
2 1
4 2
5 2
3 1
6 2
-1

1 (2 (4, 5), 3 (6))

  • 写回答

2条回答 默认 最新

  • shen_wei 2017-03-27 06:45
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划