编程介的小学生 2017-09-17 02:36 采纳率: 20.5%
浏览 870
已采纳

Digital Onion

Description

"Digital Onion (DO, for short)" consists of parentheses. Null parenthesis (no parentheses) is a DO, and " ( )" is also a DO. Then, all DOs can be formally defined recursively as follows:

Definition: Null parenthesis is a DO. Especially, we call this the Null DO.
Definition: "( )" is a DO. Especially, we call this the primitive DO.
Definition: If both A and B are DOs, then the combined form of "(A)B" is also a DO, where we call A the inside of the DO and B the outside of the DO.
For example, " ( ( ) ) ( )" is a DO and " ( ( ( ) ) ) ( ( ) ( ) ( ) )" is a DO, but " ( ) ( ) ) ( (" and " ( ( ) ) )" cannot be a DO. Let us define the weight of a DO. The weight of a DO is defined as the number of '(' symbols, or equivalently the number of ')' symbols in it. Thus, the weight of the null DO is 0, and the weight of the primitive DO " ( )" is 1. Also, it is easy to see that the weight of " ( ( ( ) ) ) ( ( ) ( ) ( ) )" is 7.

Also, if X = ( ( ( ) ) ) ( ( ) ( ) ( ) ) , the inside of X is ( ( ) ), and the outside of X is ( ( ) ( ) ( ) ). When X = ( ( ) ( ( ) ) ), the inside DO of X is ( ) ( ( ) ), and the outside DO of X should be the Null DO.

Then, we define the price order of all DO objects. The ordering rules are simple, so we can sort all DOs according to their prices. This means that we can always determine which one is the more expensive between two different DOs given.

[Rule1]: The more weight, the more expensive.
[Rule2]: If the weights of two DOs are equal, then the price depends on the inside DO of the two DOs.
[Rule3]: If the weights of two DOs are the same and the prices of the inside DOs are equal, then the price depends on the outside DO of the two DOs.
Let us explain these rules. There are two DOs, A and B. We denote A < B to show that B is more expensive than A. It is easy to see ( ) < ( ( ) ) and ( ( ) ) ( ) < ( ( ( ) ) ) ( ) by Rule 1, ( ( ) ) ( ( ) ) < ( ( ( ) ) ) ( ) by Rule 2,and ( ( ) ) ( ) ( ) < ( ( ) ) ( ( ) ) by Rule 3. Given a DO X, your task is to write the " Next More Expensive DO (NMED)" which is a more expensive DO than X, and there is no DO whose price is between NMED and X. That means that NMED is the next rank DO to a given DO when we sort all possible DOs according to their prices.
Input

The input consists of T test cases. The number of test cases (T ) is given in the first line of the input file. Each test case DO is written in a single line. '$' is placed at the end of each line to denote the end of each input DO representation, and there is at least one blank space to separate '('s and ')'s and '$'. Note that the minimum weight of input DO is 1, and the maximum weight is 30.
Output

Print exactly one line for each test case. The line should contain the Next More Expensive DO (NMED) with an ending mark '$'. As was specified in the input format, there is at least one blank space to separate '('s and ')'s and '$'.
Sample Input

3
( ) $
( ) ( ) $
( ( ) ( ( ) ) ) ( ( )( ) ) $
Sample Output

( ) ( ) $
( ( ) ) $
( ( ) ( ( ) ) ) ( ( ( ) ) ) $

  • 写回答

1条回答

  • threenewbee 2017-09-30 20:15
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀