atcoder#AGC047B. [AGC047B] First Second

[AGC047B] First Second

Score : 700700 points

Problem Statement

Limak can repeatedly remove one of the first two characters of a string, for example $abcxyx \rightarrow acxyx \rightarrow cxyx \rightarrow cyx$.

You are given NN different strings S1,S2,,SNS_1, S_2, \ldots, S_N. Among N(N1)/2N \cdot (N-1) / 2 pairs (Si,Sj)(S_i, S_j), in how many pairs could Limak obtain one string from the other?

Constraints

  • 2N2000002 \leq N \leq 200\,000
  • SiS_i consists of lowercase English letters a-z.
  • SiSjS_i \neq S_j
  • 1Si1 \leq |S_i|
  • S1+S2++SN106|S_1| + |S_2| + \ldots + |S_N| \leq 10^6

Input

Input is given from Standard Input in the following format.

NN

S1S_1

S2S_2

\vdots

SNS_N

Output

Print the number of unordered pairs (Si,Sj)(S_i, S_j) where iji \neq j and Limak can obtain one string from the other.

3
abcxyx
cyx
abc
1

The only good pair is (abcxyx,cyx)(abcxyx, cyx).

6
b
a
abc
c
d
ab
5

There are five good pairs: (b,abc)(b, abc), (a,abc)(a, abc), (abc,c)(abc, c), (b,ab)(b, ab), (a,ab)(a, ab).