311315: CF1970A1. Balanced Shuffle (Easy)

Memory Limit:256 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

A1. Balanced Shuffle (Easy)time limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

A parentheses sequence is a string consisting of characters "(" and ")", for example "(()((".

A balanced parentheses sequence is a parentheses sequence which can become a valid mathematical expression after inserting numbers and operations into it, for example "(()(()))".

The balance of a parentheses sequence is defined as the number of opening parentheses "(" minus the number of closing parentheses ")". For example, the balance of the sequence "(()((" is 3.

A balanced parentheses sequence can also be defined as a parentheses sequence with balance 0 such that each of its prefixes has a non-negative balance.

We define the balanced shuffle operation that takes a parentheses sequence and returns a parentheses sequence as follows: first, for every character of the input sequence, we compute the balance of the prefix of the sequence before that character and write those down in a table together with the positions of the characters in the input sequence, for example:

Prefix balance01212321
Position12345678
Character(()(()))

Then, we sort the columns of this table in increasing order of prefix balance, breaking ties in decreasing order of position. In the above example, we get:

Prefix balance01112223
Position18427536
Character()(()())

The last row of this table forms another parentheses sequence, in this case "()(()())". This sequence is called the result of applying the balanced shuffle operation to the input sequence, or in short just the balanced shuffle of the input sequence.

You are given a balanced parentheses sequence. Print its balanced shuffle.

Input

The only line of input contains a string $s$ consisting only of characters "(" and ")". This string is guaranteed to be a non-empty balanced parentheses sequence with its length not exceeding $500\,000$.

Output

Print the parentheses sequence $t$ — the balanced shuffle of $s$.

ExampleInput
(()(()))
Output
()(()())

Output

题目大意:
这个题目是关于平衡括号序列的重新洗牌操作。首先,对于输入序列的每个字符,我们计算该字符之前的序列前缀的平衡(即开括号数量减去闭括号数量),并将其与字符在输入序列中的位置一起记录下来。然后,我们按照前缀平衡的升序对表格的列进行排序,如果前缀平衡相同,则按照位置的降序排序。最后,表格的最后一行形成一个新的平衡括号序列,这就是输入序列的平衡洗牌结果。

输入输出数据格式:
- 输入:一个平衡括号序列,仅包含字符 "(" 和 ")",序列长度不超过 500,000。
- 输出:输入序列的平衡洗牌结果。

示例:
输入:(()(()))
输出:()(()())

请注意,这个翻译是根据您提供的网页内容进行的,可能存在一定的误差。如果需要更精确的翻译,建议您提供完整的网页内容。题目大意: 这个题目是关于平衡括号序列的重新洗牌操作。首先,对于输入序列的每个字符,我们计算该字符之前的序列前缀的平衡(即开括号数量减去闭括号数量),并将其与字符在输入序列中的位置一起记录下来。然后,我们按照前缀平衡的升序对表格的列进行排序,如果前缀平衡相同,则按照位置的降序排序。最后,表格的最后一行形成一个新的平衡括号序列,这就是输入序列的平衡洗牌结果。 输入输出数据格式: - 输入:一个平衡括号序列,仅包含字符 "(" 和 ")",序列长度不超过 500,000。 - 输出:输入序列的平衡洗牌结果。 示例: 输入:(()(())) 输出:()(()()) 请注意,这个翻译是根据您提供的网页内容进行的,可能存在一定的误差。如果需要更精确的翻译,建议您提供完整的网页内容。

加入题单

上一题 下一题 算法标签: