#D13262. san
san
san
C --Misawa's rooted tree
Problem Statement
You noticed that your best friend Misawa's birthday was near, and decided to give him a rooted binary tree as a gift. Here, the rooted binary tree has the following graph structure. (Figure 1)
- Each vertex has exactly one vertex called the parent of that vertex, which is connected to the parent by an edge. However, only one vertex, called the root, has no parent as an exception.
- Each vertex has or does not have exactly one vertex called the left child. If you have a left child, it is connected to the left child by an edge, and the parent of the left child is its apex.
- Each vertex has or does not have exactly one vertex called the right child. If you have a right child, it is connected to the right child by an edge, and the parent of the right child is its apex.
Figure 1. An example of two rooted binary trees and their composition
You wanted to give a handmade item, so I decided to buy two commercially available rooted binary trees and combine them on top of each other to make one better rooted binary tree. Each vertex of the two trees you bought has a non-negative integer written on it. Mr. Misawa likes a tree with good cost performance, such as a small number of vertices and large numbers, so I decided to create a new binary tree according to the following procedure.
- Let the sum of the integers written at the roots of each of the two binary trees be the integers written at the roots of the new binary tree.
- If both binary trees have a left child, create a binary tree by synthesizing each of the binary trees rooted at them, and use it as the left child of the new binary tree root. Otherwise, the root of the new bifurcation has no left child.
- If the roots of both binaries have the right child, create a binary tree by synthesizing each of the binary trees rooted at them, and use it as the right child of the root of the new binary tree. Otherwise, the root of the new bifurcation has no right child.
You decide to see what the resulting rooted binary tree will look like before you actually do the compositing work. Given the information of the two rooted binary trees you bought, write a program to find the new rooted binary tree that will be synthesized according to the above procedure.
Here, the information of the rooted binary tree is expressed as a character string in the following format.
(String representing the left child) [Integer written at the root] (String representing the right child)
A tree without nodes is an empty string. For example, the synthesized new rooted binary tree in Figure 1 is (() [6] ()) [8] (((() [4] ()) [7] ()) [9] () )
Written as.
Input
The input is expressed in the following format.
and are character strings that represent the information of the rooted binary tree that you bought, and the length is or more and or less. The information given follows the format described above and does not include extra whitespace. Also, rooted trees that do not have nodes are not input. You can assume that the integers written at each node are greater than or equal to and less than or equal to . However, note that the integers written at each node of the output may not fall within this range.
Output
Output the information of the new rooted binary tree created by synthesizing the two rooted binary trees in one line. In particular, be careful not to include extra whitespace characters other than line breaks at the end of the line.
Sample Input 1
((() [8] ()) [2] ()) [5] (((() [2] ()) [6] (() [3] ())) [1] ()) (() [4] ()) [3] (((() [2] ()) [1] ()) [8] (() [3] ()))
Output for the Sample Input 1
(() [6] ()) [8] (((() [4] ()) [7] ()) [9] ())
Sample Input 2
(() [1] ()) [2] (() [3] ()) (() [1] (() [2] ())) [3] ()
Output for the Sample Input 2
(() [2] ()) [5] ()
Sample Input 3
(() [1] ()) [111] () () [999] (() [9] ())
Output for the Sample Input 3
() [1110] ()
Sample Input 4
(() [2] (() [4] ())) [8] ((() [16] ()) [32] (() [64] ())) ((() [1] ()) [3] ()) [9] (() [27] (() [81] ()))
Output for the Sample Input 4
(() [5] ()) [17] (() [59] (() [145] ()))
Sample Input 5
() [0] () () [1000] ()
Output for the Sample Input 5
() [1000] ()
Example
Input
Output
inputFormat
Input
The input is expressed in the following format.
and are character strings that represent the information of the rooted binary tree that you bought, and the length is or more and or less. The information given follows the format described above and does not include extra whitespace. Also, rooted trees that do not have nodes are not input. You can assume that the integers written at each node are greater than or equal to and less than or equal to . However, note that the integers written at each node of the
outputFormat
output may not fall within this range.
Output
Output the information of the new rooted binary tree created by synthesizing the two rooted binary trees in one line. In particular, be careful not to include extra whitespace characters other than line breaks at the end of the line.
Sample Input 1
((() [8] ()) [2] ()) [5] (((() [2] ()) [6] (() [3] ())) [1] ()) (() [4] ()) [3] (((() [2] ()) [1] ()) [8] (() [3] ()))
Output for the Sample Input 1
(() [6] ()) [8] (((() [4] ()) [7] ()) [9] ())
Sample Input 2
(() [1] ()) [2] (() [3] ()) (() [1] (() [2] ())) [3] ()
Output for the Sample Input 2
(() [2] ()) [5] ()
Sample Input 3
(() [1] ()) [111] () () [999] (() [9] ())
Output for the Sample Input 3
() [1110] ()
Sample Input 4
(() [2] (() [4] ())) [8] ((() [16] ()) [32] (() [64] ())) ((() [1] ()) [3] ()) [9] (() [27] (() [81] ()))
Output for the Sample Input 4
(() [5] ()) [17] (() [59] (() [145] ()))
Sample Input 5
() [0] () () [1000] ()
Output for the Sample Input 5
() [1000] ()
Example
Input
Output
样例
((()[8]())[2]())[5](((()[2]())[6](()[3]()))[1]())
(()[4]())[3](((()[2]())[1]())[8](()[3]()))
(()[6]())[8](((()[4]())[7]())[9]())