eolymp
bolt
Try our new interface for solving problems
Problems

Mix and Build

Mix and Build

In this problem, you are given a list of words (sequence of lower case letters). From this list, find the longest chain of words \textbf{w_1}, ..., \textbf{w_n} such that \textbf{w_i} is a \textit{mixed extension} of \textbf{w_\{i-1\}}. A word \textbf{A} is a mixed extension of another word \textbf{B} if \textbf{A} can be formed by adding one letter to \textbf{B} and permuting the result. For example, "\textbf{ab}", "\textbf{bar}", "\textbf{crab}", "\textbf{cobra}", and "\textbf{carbon}" form a chain of length \textbf{5}. \InputFile Each input block contains at least two, but no more than \textbf{10000} lines. Each line contains a word. The length of each word is at least \textbf{1} and no more than \textbf{20}. All words in the input are distinct. \OutputFile Write the longest chain that can be constructed from the given words. Output each word in the chain on a separate line, starting from the first one. If there are multiple longest chains, any longest chain is acceptable.
Time limit 2 seconds
Memory limit 64 MiB
Input example #1
ab
arc
arco
bar
bran
carbon
carbons
cobra
crab
crayon
narc
Output example #1
ab
bar
crab
cobra
carbon
carbons