#abc353e. E - Yet Another Sigma Problem

E - Yet Another Sigma Problem

Score: 500500 points

问题陈述

对于字符串 xxyy,定义 f(x,y)f(x, y) 如下:

  • f(x,y)f(x, y)xxyy 的最长公共前缀的长度。

给定由小写英文字母组成的 NN 个字符串 (S1,,SN)(S_1, \ldots, S_N)。找出以下表达式的值:

$\displaystyle \sum_{i=1}^{N-1}\sum_{j=i+1}^N f(S_i,S_j)$。

以上为大语言模型 kimi 翻译,仅供参考。

Problem Statement

For strings xx and yy, define f(x,y)f(x, y) as follows:

  • f(x,y)f(x, y) is the length of the longest common prefix of xx and yy.

You are given NN strings (S1,,SN)(S_1, \ldots, S_N) consisting of lowercase English letters. Find the value of the following expression:

$\displaystyle \sum_{i=1}^{N-1}\sum_{j=i+1}^N f(S_i,S_j)$.

Constraints

  • 2N3×1052 \leq N \leq 3\times 10^5
  • SiS_i is a string consisting of lowercase English letters.
  • 1Si1 \leq |S_i|
  • S1+S2++SN3×105|S_1|+|S_2|+\ldots+|S_N|\leq 3\times 10^5
  • All input numbers are integers.

Input

The input is given from Standard Input in the following format:

NN

S1S_1 \ldots SNS_N

Output

Print the answer.

Sample Input 1

3
ab abc arc

Sample Output 1

4
  • f(S1,S2)=2f(S_1,S_2)=2
  • f(S1,S3)=1f(S_1,S_3)=1
  • f(S2,S3)=1f(S_2,S_3)=1

Thus, the answer is f(S1,S2)+f(S1,S3)+f(S2,S3)=4f(S_1,S_2) + f(S_1,S_3) + f(S_2,S_3) = 4.

Sample Input 2

11
ab bb aaa bba baba babb aaaba aabbb a a b

Sample Output 2

32