P18085 [UKIEPC 2026] Hoptimisation

Description

You are practicing on a somewhat tedious hopscotch course. Several squares are arranged in a straight line, with adjacent squares $1$ meter apart. You have already planned the sequence of square names to jump to. The total jumping distance depends on which actual position, numbered from $1$ to $k$, you assign to each name. For example, if the squares from left to right are named `A`, `B`, `C`, `D`, and `E`, jumping in the order `A`, `B`, `A`, `C` covers a total distance of $1+1+2=4$ meters. ![image.png](https://cdn.luogu.com.cn/upload/image_hosting/7dcnfrmu.webp) *Figure H.1: Illustration of Sample 1.* Given the jumping sequence, determine the left-to-right arrangement of the names that minimizes the total jumping distance.

Input Format

The first line contains two integers: the number of squares $k$ ($1 \le k \le 16$) and the number of jumps $n$ ($1 \le n \le 10^6$). The second line contains $n$ strings consisting of lowercase letters. The $i$th string $s_i$ is the name of the target square for the $i$th jump ($1 \le |s_i| \le 10$). It is guaranteed that the input contains at most $k$ distinct square names.

Output Format

Output $k$ strings, each consisting of at most $10$ lowercase letters, representing the names of the squares from left to right. If there are multiple solutions, output any of them. If the input gives fewer than $k$ distinct square names, you may choose names yourself to fill the remaining squares.

Explanation/Hint

Translated by QwenWork