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:

    {][[[[{}[] 
    

    IMPOSSIBLE

    Ideas:

    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.



  • The following situations can be encountered in the application of the sampling algorithm (except for each pair):

    1. The whole line - the opening brackets remained in the glass;
    2. Passed to the closing brackets - empty glass;
    3. 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.

    Completed

    Let right. ABC and D empty or balanced expressions.

    1. If we have an expression A(B in 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. B[C and A(B[C to A(B♪ Any similar expression can thus be balanced;

    2. If we have an expression A)B The 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]C and more closed;

    3. If there is an expression A(B[C)D or A(B]C)D If 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.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2