#K13091. Count Distinct Substrings

    ID: 23835 Type: Default 1000ms 256MiB

Count Distinct Substrings

Count Distinct Substrings

Given a string S, your task is to count the number of distinct substrings in S. A substring is defined as a contiguous sequence of characters from S. For example, if S = "abcab" then the distinct substrings are:

"a", "b", "c", "ab", "bc", "ca", "abc", "bca", "cab", "abca", "bcab", "abcab"

You need to output the total count of these distinct substrings.

Note that the empty substring is not considered as a valid substring.

The problem can be formulated as: Given a string \( S \) of length \( n \), count the number of distinct substrings. The total number of substrings of a string is \( \frac{n(n+1)}{2} \), but many of them may be repeated. Your task is to count only the unique ones.

inputFormat

The input consists of a single line containing the string S. The string may include lowercase and/or uppercase letters. Note that S can be empty.

outputFormat

Output a single integer representing the number of distinct substrings of S.

## sample
abcab
12