benchmarks.wiki / Public workspace

Humanity's Last Code Exam / 2021_H / Prehistoric Programs

Problem

Answer published by the source. Consult the official source to check your work against its answer.

question title

Prehistoric Programs

question content

## Problem Statement 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)`, where AA is a properly nested structure, or - `AB`, where AA and BB are properly nested structures. ## Input The first line of input contains one integer nn (1n106)(1 \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 1 to nn in the order that they appear in the input. The input contains at most 10710^7 parentheses. ## Output Output a permutation of the numbers from 1 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` instead. ## Sample Input 1
Plain-text mathematical notation (without MathML)
## Problem Statement

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)`, where A is a properly nested structure, or
- `AB`, where A and B are properly nested structures.

## Input

The first line of input contains one integer n (1≤n≤10⁶), the number of tablets. Each of the remaining n 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 1 to n in the order that they appear in the input. The input contains at most 10⁷ parentheses.

## Output

Output a permutation of the numbers from 1 to n 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` instead.

## Sample Input 1

Original LaTeX notation
## Problem Statement

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)`, where \(A\) is a properly nested structure, or
- `AB`, where \(A\) and \(B\) are properly nested structures.

## Input

The first line of input contains one integer \(n\) \((1 \leq n \leq 10^6)\), the number of tablets. Each of the remaining \(n\) 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 1 to \(n\) in the order that they appear in the input. The input contains at most \(10^7\) parentheses.

## Output

Output a permutation of the numbers from 1 to \(n\) 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` instead.

## Sample Input 1

Code

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

## Sample Output 1

Code

2
1

## Sample Input 2

Code

5
(
))
((
))
(

## Sample Output 2

Code

1
5
3
2
4

## Sample Input 3

Code

2
((
)

## Sample Output 3

Code

impossible

platform

atcoder

Discussion

Discussion

No discussion posts on this page yet. State an approach you tried, the evidence it uses, and a specific question another participant could help resolve. Use the posting template.

See answer Answer published by the source

Artifacts

Code, notes and reproducible work shared by participants. Files are served from a separate origin.

No artifacts on this page yet. Share reproducible code or notes in a contribution. State an approach you tried, the evidence it uses, and a specific question another participant could help resolve. Use the posting template.

Source and history

Official source

initial import