Target: Is it possible to continue the fragment on both sides?
Data algorithms and structure challenge.
Dun fragment of the sequence of the brackets consisting of symbols
It is necessary to determine whether the fragment may be continued on both sides with an appropriate sequence. If you may have a minimum correct sequence,, otherwise, print IMPOSSIBLE. The maximum length of line 10^6 symbols.
Sample Input 1:
Sample Output 1:
Sample Input 2:
There's a standard algorithm of checking the correctness of the bracketing through the stack. Subject matter:
- Let's go in line, if we meet the opening bracket, it's in the glass.
- If we meet the closing bracket and the top of the stack is similar to its opening, then we remove it from the stream.
- If, after the whole line passes empty, the brackets are correct.
This algorithm algorithm should be modified to enable (or to determine) the addition of the verifiable line to the correct one.
To that end, he tried to put the counters on every bracket and its direction, and kept The opposite of the bracket in the deca and at the right moment extract from the deca. But as long as it's covered in need and algorithm, it's still not working right.
irl last edited by
The following situations can be encountered in the application of the sampling algorithm (except for each pair):
- The whole line - the opening brackets remained in the glass;
- Passed to the closing brackets - empty glass;
- We reached the closing brackets, another bracket on the top of the glass.
In the first case, your right line can be supplemented to obligate the brackets. In the second case, the brackets on the left can be added to forcefully open them. In order to correct the third case, the brackets will have to be placed in the middle of the line.
Dempty or balanced expressions.
If we have an expression
A(Bin the glass, there will be an opening bracket and it can be closed by adding to the line of the closing bracket. If we have an expression
A(B[Cwe can also make a balanced expression.
A(B♪ Any similar expression can thus be balanced;
If we have an expression
A)BThe check will attempt to close the unopened brackets (no open brackets will be available in the glass). The expression can be corrected by adding the expression on the left of the opening bracket. The same balance of expression
A)B]Cand more closed;
If there is an expression
A(B]C)DIf the closing brackets are received at the top of the glass, a bracket which does not correspond to the opening will be found. This situation can only be rectified within the outer brackets, and it is therefore impossible to correct this expression by adding the brackets to the left or right.