#P6139. 【模板】广义后缀自动机(广义 SAM)

【模板】广义后缀自动机(广义 SAM)

Description

Given nn strings s1,s2,,sns_1, s_2, \ldots, s_n consisting of lowercase letters, find the number of distinct substrings (excluding the empty string).

Furthermore, let QQ be the minimal DFA that can accept all suffixes of s1,s2,,sns_1, s_2, \dots, s_n. Please output the number of states in QQ. (If you cannot understand this sentence, you may treat it as outputting the number of states of the "generalized suffix automaton" built from s1,s2,,sns_1, s_2, \dots, s_n.)

Input Format

The first line contains a positive integer nn.

The next nn lines each contain a string. The ii-th line represents the string si1s_{i-1}.

Output Format

The first line contains a positive integer: the number of distinct substrings.

The second line contains a positive integer: the number of states in QQ.

4
aa
ab
bac
caa
10
10
1
a
1
2

Hint

Constraints: 1n41051 \le n \le 4 \cdot 10^5, 1si1061 \le \sum{|s_i|} \le 10^6.

Sample 1 explanation: There are 1010 distinct substrings: "a","b","c","aa","ab","ac","ba","ca","bac","caa". The DFA structure is omitted.

Sample 2 explanation: There is 11 distinct substring: "a". The DFA contains a start state SS and a state TT, with an edge STS \to T labeled a. Therefore, the total number of states is 22.

Translated by ChatGPT 5