#P1241. 括号序列

括号序列

Description

Define the following rules:

  1. The empty string is a "balanced bracket sequence".
  2. If string SS is a "balanced bracket sequence", then [S]\texttt{[}S\texttt{]} and (S)\texttt{(}S\texttt{)} are also "balanced bracket sequences".
  3. If strings AA and BB are both "balanced bracket sequences", then ABAB (the concatenation of the two strings) is also a "balanced bracket sequence".

For example, the following strings are balanced bracket sequences:

  • (), [], (()), ([]), ()[], ()[()].

The following are not:

  • (, [, ], )(, ()), ([().

Now, given a string ss consisting only of (, ), [, ], please pair the characters in the string as follows:

  1. Scan the string from left to right.
  2. For the current character, if it is a right bracket, look at the nearest unmatched left bracket to its left. If they correspond (parenthesis matches parenthesis, square bracket matches square bracket), then pair them. If there is no unmatched left bracket to its left or they do not correspond, then pairing fails.

After pairing ends, for every unmatched bracket in ss, add one character next to it so that this bracket matches with the newly added one.

Input Format

The input contains a single line with the string ss.

Output Format

Output a single line with the resulting string.

([()

()[]()

([)
()[]()

Hint

Constraints

For all test points, it is guaranteed that the length of ss does not exceed 100100, and ss contains only the four characters (, ), [, ].

Translated by ChatGPT 5