#P1127. 词链

词链

Description

If the last letter of word XX is the same as the first letter of word YY, then XX and YY can be concatenated as X.YX.Y. Note: there is an English period . between XX and YY. For example, for the words dog and gopher, dog and gopher can be concatenated as dog.gopher.

More examples:

  • dog.gopher
  • gopher.rat
  • rat.tiger
  • aloha.aloha
  • arachnid.dog

A concatenated word can be connected with other words to form a longer word chain, for example:

aloha.arachnid.dog.gopher.rat.tiger

Note that the letters on both sides of . must be the same.

Now you are given some words. Please find the lexicographically smallest word chain such that each word appears in the chain exactly once. Note that if the same word appears kk times, you must output it kk times.

Input Format

The first line contains a positive integer nn (1n10001 \le n \le 1000), representing the number of words.

The next nn lines each contain a word consisting of 11 to 2020 lowercase letters.

Output Format

Output a single line: the lexicographically smallest word chain that uses every given word exactly once. If such a chain does not exist, output exactly three asterisks ***.

6
aloha
arachnid
dog
gopher
rat
tiger
aloha.arachnid.dog.gopher.rat.tiger

Hint

  • For 40%40\% of the testdata, n10n \le 10.
  • For 100%100\% of the testdata, n1000n \le 1000.

Translated by ChatGPT 5