编程介的小学生 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 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程