아직 만들고 있는 페이지입니다.

이 페이지는 아직 만드는 중입니다. 보이는 내용은 바뀔 수 있습니다.

Pen

면접 대비

시간 제한1초메모리 제한128 MB

요약
괄호 문자열이 주어질 때, 앞과 뒤에 최소 개수의 괄호를 붙여 올바른 괄호열로 만들고, 불가능하면 NIE를 출력한다.
난이도

보통10점 중 5점

유형
스택, 그리디, 구현
정답자
아직 제출이 없습니다

문제

Johnny loves order and is very upset by untidy parentheses. Lately he saw a string "){([]", which annoyed him since it was not a correct bracket sequence. Fortunately, Johnny had a pen in his pocket and he could add '(' at the beginning and ')}' at the end. Then he could sleep soundly again, since the string "(){([])}" is a correct bracket sequence.

Johnny took on repairing bracket sequences to be his life's mission. He will do this by adding new parentheses at the beginning and at the end of sequences he finds, so as to make them correct. Since his pen is running out of ink, Johnny would like to add the least possible number of brackets which will accomplish this. Help him! Write a program which will read a bracket sequence and either compute a shortest possible corrected bracket sequence for it and write it to standard output, or say that there is no way to correct it.

Correct bracket sequences are defined recursively as follows:

  • the empty sequence is a correct bracket sequence,
  • if SS and TT are correct bracket sequences, then their concatenation STST is also a correct bracket sequence,
  • if SS is a correct bracket sequence, then (S)(S), \[S]\[S] and S\\{S\\} are all also correct bracket sequences.

입력

The first and only line of input contains a nonempty sequence consisting of characters (, ), [, ], {, } and having length at most 1,000,0001\\,000\\,000; this is the bracket sequence that Johnny wants corrected.

출력

The output should contain a corrected input sequence. If there are many possible ways to correct the input sequence that have the same minimum length, output any of them.

If it is impossible to correct the input sequence, output the word NIE.

힌트

First example is the one from the problem statement.

In the second example, sequence cannot be repaired.

예제2

  1. 예제 1

    입력
    ){([]
    
    예상 출력
    (){([])}
    
  2. 예제 2

    입력
    ([]}
    
    예상 출력
    NIE