编程介的小学生 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 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog