All problems

Problem 532

Count Distinct Substrings

Hard · 30 pts · String, Suffix Automaton

Problem

Count the number of different non-empty substrings of a string S. Two substrings are the same if they consist of the same characters in the same order, no matter where they occur. With |S| up to 100000 there can be billions of substrings, so listing them is impossible. Build a suffix automaton: every state v adds len(v) - len(link(v)) new substrings, and the sum over all states is the answer.

Input

A single string S of lowercase English letters.

Output

Print the number of distinct non-empty substrings.

Constraints

1 <= |S| <= 100000

Example input

aba

Example output

5

Explanation

The substrings of aba are a, b, a, ab, ba and aba. The letter a appears twice but counts once, so there are 5 distinct substrings.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Minimum Window Subsequence Next: Longest Duplicate Substring