编程介的小学生 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 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用