#P7537. [COCI 2016/2017 #4] Rima

[COCI 2016/2017 #4] Rima

Description

Define the length of the longest common suffix of strings A,BA, B as LCS(A,B)\text{LCS}(A,B).

When LCS(A,B)max(A,B)1\text{LCS}(A,B) \ge \max(|A|,|B|)-1, we say that the two strings A,BA, B rhyme.

Given NN strings, you need to form a longest possible sequence of strings (the sequence length is the number of strings in it) such that every pair of adjacent strings in the sequence rhymes.

Input Format

The first line contains an integer NN.

The next NN lines each contain one string. It is guaranteed that all strings are distinct, consist of lowercase letters, and the total length does not exceed 3×1063 \times 10^6.

Output Format

Output the maximum possible length of the string sequence.

4
honi
toni
oni
ovi
3
5
ask
psk
krafna
sk
k
4
5
pas
kompas
stas
s
nemarime
1

Hint

[Explanation of Sample 2]

The string sequence ask-psk-sk-k\texttt{ask-psk-sk-k} has the maximum length, which is 44.

[Explanation of Sample 3]

No two strings rhyme, so any single string can form a sequence by itself. The answer is 11.

[Constraints]

For 30%30\% of the testdata, N18N \le 18.

For 100%100\% of the testdata, 1N5×1051 \le N \le 5 \times 10^5.

[Hints and Notes]

This problem is translated from COCI 2016-2017 CONTEST #4 T5 Rima.

The score of this problem follows the original COCI setting, with a full score of 140140.

Translated by ChatGPT 5