How Many Distinct Substrings — Solution & Editorial
Every substring is a prefix of some suffix. Sorting suffixes (suffix array) and subtracting adjacent LCPs removes the double-counted shared prefixes: answer = ∑(n−sa[i]) − ∑lcp[i] = n(n+1)/2 − ∑lcp.
Complexity: O(n log n)
Watch out: The count can reach ~5×109 — 64-bit.