Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
Language: Dr. Podboq or: How We Became Asymmetric
Description After long studying how embryos of organisms become asymmetric during their development, Dr. Podboq, a famous biologist, has reached his new hypothesis. Dr. Podboq is now preparing a poster for the coming academic conference, which shows a tree representing the development process of an embryo through repeated cell divisions starting from one cell. Your job is to write a program that transforms given trees into forms satisfying some conditions so that it is easier for the audience to get the idea. A tree representing the process of cell divisions has a form described below.
Figure F-1: A tree representing a process of cell divisions According to Dr. Podboq's hypothesis, we can determine which cells have stronger or weaker asymmetricity by looking at the structure of this tree representation. First, his hypothesis defines "left-right similarity" of cells as follows:
Figure F-2: An example tree The left-right similarity of the cell A is computed as follows. First, within the descendants of the cell B, which is the left child cell of A, the following three kinds of structures appear. Notice that the rightmost structure appears three times, but when we count the number of structures, we count it only once. Figure F-3: Structures appearing within the descendants of the cell B On the other hand, within the descendants of the cell C, which is the right child cell of A, the following four kinds of structures appear. Figure F-4: Structures appearing within the descendants of the cell C Among them, the first, second, and third ones within the B side are regarded as the same structure as the second, third, and fourth ones within the C side, respectively. Therefore, there are four structures in total, and three among them are common to the left side and the right side, which means the left-right similarity of A is 3/4. Given the left-right similarity of each cell, Dr. Podboq's hypothesis says we can determine which of the cells X and Y has stronger asymmetricity by the following rules.
Now, your job is to write a program that transforms a given tree representing a process of cell divisions, by interchanging two child cells of arbitrary cells, into a tree where the following conditions are satisfied.
For example, suppose we are given the tree in Figure F-2. First we compare B and C, and because B has lower left-right similarity, which means stronger asymmetricity, we keep B at left and C at right. Next, because B is the left child cell of A, we compare two child cells of B, and the one with stronger asymmetricity is positioned at left. On the other hand, because C is the right child cell of A, we compare two child cells of C, and the one with stronger asymmetricity is positioned at right. We examine the other cells in the same way, and the tree is finally transformed into the tree shown below. Figure F-5: The example tree after the transformation Please be warned that the only operation allowed in the transformation of a tree is to interchange two child cells of some parent cell. For example, you are not allowed to transform the tree in Figure F-2 into the tree below. Figure F-6: An example of disallowed transformation Input The input consists of n lines (1≤n≤100) describing n trees followed by a line only containing a single zero which represents the end of the input. Each tree includes at least 1 and at most 127 cells. Below is an example of a tree description. ((x (x x)) x) This description represents the tree shown in Figure F-1. More formally, the description of a tree is in either of the following two formats. "(" <description of a tree starting at the left child> <single space> <description of a tree starting at the right child> ")"or "x"The former is the description of a tree whose starting cell has two child cells, and the latter is the description of a tree whose starting cell has no child cell. Output For each tree given in the input, print a line describing the result of the tree transformation. In the output, trees should be described in the same formats as the input, and the tree descriptions must appear in the same order as the input. Each line should have no extra character other than one tree description. Sample Input (((x x) x) ((x x) (x (x x)))) (((x x) (x x)) ((x x) ((x x) (x x)))) (((x x) ((x x) x)) (((x (x x)) x) (x x))) (((x x) x) ((x x) (((((x x) x) x) x) x))) (((x x) x) ((x (x x)) (x (x x)))) ((((x (x x)) x) (x ((x x) x))) ((x (x x)) (x x))) ((((x x) x) ((x x) (x (x x)))) (((x x) (x x)) ((x x) ((x x) (x x))))) 0 Sample Output ((x (x x)) ((x x) ((x x) x))) (((x x) ((x x) (x x))) ((x x) (x x))) (((x ((x x) x)) (x x)) ((x x) ((x x) x))) (((x ((x ((x x) x)) x)) (x x)) ((x x) x)) ((x (x x)) ((x (x x)) ((x x) x))) (((x (x x)) (x x)) ((x ((x x) x)) ((x (x x)) x))) (((x (x x)) ((x x) ((x x) x))) (((x x) (x x)) (((x x) (x x)) (x x)))) Source |
[Submit] [Go Back] [Status] [Discuss]
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator