当前你的浏览器版本过低,网站已在兼容模式下运行,兼容模式仅提供最小功能支持,网站样式可能显示不正常。
请尽快升级浏览器以体验网站在线编辑、在线运行等功能。

建议使用的浏览器:

谷歌Chrome 火狐Firefox Opera浏览器 微软Edge浏览器 QQ浏览器 360浏览器 傲游浏览器

1335:Digital Onion

题目描述
"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.
输入解释
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.
输出解释
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 '$'.
输入样例
3
( ) $
( ) ( ) $
( ( ) ( ( ) ) ) ( ( )( ) ) $
输出样例
( ) ( ) $
( ( ) ) $
( ( ) ( ( ) ) ) ( ( ( ) ) ) $


该题目是Virtual Judge题目,来自 北京大学POJ

题目来源 Taejon 2002

源链接: POJ-1335

最后修改于 2020-10-29T06:01:15+00:00 由爬虫自动更新

共提交 0

通过率 --%
时间上限 内存上限
1000 10000