#P4471. [BJWC2018] 词韵

[BJWC2018] 词韵

Description

Adrian likes rhyme in poetry. He thinks two words rhyme if and only if the length of their longest common suffix is at least the length of the longer word minus one. That is, words A and B rhyme if and only if LCS(A,B)max(A,B)1\operatorname{LCS}(A,B) \ge \max(|A|,|B|)- 1. (Here, LCS\operatorname{LCS} is the abbreviation for the longest common suffix.)

Now Adrian has NN words. He wants to select as many words as possible to form a sequence such that any two adjacent words in the sequence rhyme.

Input Format

The first line contains an integer NN.

Each of the next NN lines contains a string consisting of lowercase English letters, representing a word. All words are distinct.

Output Format

Output a single line with one integer, the length of the longest word sequence.

5
ask
psk
k
krafna
sk
4

Hint

[Sample Explanation]

One longest word sequence is ask-psk-sk-k.

[Constraints]

30% of the testdata: 1N201 \le N \le 20, and the sum of word lengths does not exceed 30003000.

100% of the testdata: 1N5×1051 \le N \le 5 \times 10^5, and the sum of word lengths does not exceed 3×1063 \times 10^6.

Translated by ChatGPT 5