I would start very simply. Make 2 line vectors, one of which is sorted normally, one of them is sorted by the last two letters. Create an index (vector ints) for the second vector, which indicates its position in the first.
To find the longest, first remove the orphans. words that do not match with anything. Then you want to build an adjacent tree joint, this is where you determine which words can reach each other. If you have two or more trees, first try the largest tree.
Now with the tree, your task is to find ends that are rare, and snap them to other ends and repeat. This should make you quite a pleasant decision, if it uses all the words of your golden, skip other trees. If this does not mean that you have introduced many algorithms to make it effective.
Some points to consider: If you have 3+ unique endings, you are guaranteed to drop 1+ words. This can be used to trim your attempts to find the answer. often repeat unique ends. The odd numbers of the given end guarantee that you need to drop it (you get 2 freebies at the ends). Separate words that can be combined, you can always drop them last, and otherwise they will ruin the math. You can create small self-consistent rings, you can treat them as matching words, if you do not force them into orphans when they are created. This may make the performance fantastic, but it does not guarantee the perfect solution.
Search space is an order (N!), A list of millions of elements can be difficult for an exact answer. Of course, I could forget something.
Storm source share