## ICPC NAQ 2016 D - Brackets

View as PDF

Points: 10
Time limit: 0.6s
Memory limit: 1G

Problem type
Allowed languages
Ada, Assembly, Awk, Brain****, C, C#, C++, COBOL, CommonLisp, D, Dart, F#, Forth, Fortran, Go, Groovy, Haskell, Intercal, Java, JS, Kotlin, Lisp, Lua, Nim, ObjC, OCaml, Octave, Pascal, Perl, PHP, Pike, Prolog, Python, Racket, Ruby, Rust, Scala, Scheme, Sed, Swift, TCL, Text, Turing, VB, Zig
##### ICPC North America Qualifier 2016, Problem D

A bracket sequence consisting of ( and ) is defined to be valid as follows:

• An empty sequence is valid.
• If is a valid bracket sequence, then is a valid bracket sequence.
• If and are valid bracket sequences, then the concatenation of and , , is a valid bracket sequence.

For example, (()), ()(), and (()())() are all valid bracket sequences, while ( and ()) are invalid bracket sequences.

You get a bracket sequence from the professor of length . However, it might not be valid at the moment. The professor asks you to check if it is possible to make the sequence valid by performing at most one segment inversion operation. That is, you may choose two 1-based indices and and invert each bracket with an index in the closed interval . After the inversion, a left bracket ( becomes a right bracket ), and a right bracket ) becomes a left bracket (.

You can make ())( valid by inverting the segment . You can make ())) valid by inverting the segment , or alternatively by inverting the segment . However, there does not exist a segment you can invert to make )))( valid.

#### Input Specification

The input consists of one line containing between and brackets.

#### Output Specification

Output possible if you can make the bracket sequence valid by performing at most one segment inversion, or impossible otherwise.

#### Sample Input 1

()))

#### Sample Output 1

possible

#### Sample Input 2

)))(

#### Sample Output 2

impossible

#### Sample Input 3

()

#### Sample Output 3

possible