Rhyme
Time limit1sMemory limit256 MB
Given N distinct words, find the longest sequence using each word at most once where consecutive words rhyme: their longest common suffix has length at least the longer word's length minus one.
Problem
Adrian likes rhymes. He decides that two words rhyme if and only if the length of their longest common suffix equals the length of the longer word, or falls exactly 1 short of it. In other words, words and rhyme if and only if
Here is the length of the longest common suffix of and , and is the length of .
One day, while reading a collection of short stories, Adrian decided to lay out the longest possible sequence of words in which every two consecutive words rhyme. The words come from the given list, and no word may be used twice.
Adrian got tired of the task and went back to reading. Given words, write a program that computes the maximum length of such a sequence.
Input
The first line contains the number of words ().
Each of the next lines contains one word. Every word consists of lowercase English letters, and all words are distinct. The sum of the word lengths is at most .
Output
Print the length of the longest sequence.