#P9446. [ICPC2021 WF] Prehistoric Programs

[ICPC2021 WF] Prehistoric Programs

题目描述

Archaeologists have discovered exciting clay tablets in deep layers of Alutila Cave. Nobody was able to decipher the script on the tablets, except for two symbols that seem to describe nested structures not unlike opening and closing parentheses in LISP. Could it be that humans wrote programs thousands of years ago?

Taken together, the tablets appear to describe a great piece of work - perhaps a program, or an epic, or even tax records! Unsurprisingly, after such a long time, the tablets are in a state of disorder. Your job is to arrange them into a sequence so that the resulting work has a properly nested parenthesis structure. Considering only opening and closing parentheses, a properly nested structure is either

  • ()(), or
  • (A)(A), where AA is a properly nested structure, or
  • ABAB, where AA and BB are properly nested structures.

输入格式

The first line of input contains one integer nn (1n1061 \leq n \leq 10^6), the number of tablets. Each of the remaining nn lines describes a tablet, and contains a non-empty string of opening and closing parentheses; symbols unrelated to the nesting structure are omitted. The strings are numbered from 11 to nn in the order that they appear in the input. The input contains at most 10710^7 parentheses.

输出格式

Output a permutation of the numbers from 11 to nn such that concatenating the strings in this order results in a properly nested structure. If this happens for multiple permutations, any one of them will be accepted. If there is no such permutation, output impossible\texttt{impossible} instead.

题目大意

给定 nn 个括号序列,要求以某个顺序排列这些序列后,连接起来,整个括号序是合法匹配的。输出连接顺序,无解输出 impossible

translated by liangbowen.

2
())())()
((()

2
1
5
(
))
((
))
(

1
5
3
4
2

2
((
)

impossible