P1241 Bracket Sequence
Description
Define the following rules:
1. The empty string is a "balanced bracket sequence".
2. If string $S$ is a "balanced bracket sequence", then $\texttt{[}S\texttt{]}$ and $\texttt{(}S\texttt{)}$ are also "balanced bracket sequences".
3. If strings $A$ and $B$ are both "balanced bracket sequences", then $AB$ (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 $s$ 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 $s$, 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 $s$.
Output Format
Output a single line with the resulting string.
Explanation/Hint
Constraints
For all test points, it is guaranteed that the length of $s$ does not exceed $100$, and $s$ contains only the four characters `(`, `)`, `[`, `]`.
Translated by ChatGPT 5