#P1127. 词链
词链
Description
If the last letter of word is the same as the first letter of word , then and can be concatenated as . Note: there is an English period . between and . For example, for the words dog and gopher, dog and gopher can be concatenated as dog.gopher.
More examples:
dog.gophergopher.ratrat.tigeraloha.alohaarachnid.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 times, you must output it times.
Input Format
The first line contains a positive integer (), representing the number of words.
The next lines each contain a word consisting of to 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 of the testdata, .
- For of the testdata, .
Translated by ChatGPT 5
京公网安备 11011102002149号