Codeforces Round 869 (Div. 1)
F
Entangled Substrings
Codeforces
3500
原题
← E→
翻译中…

Quantum entanglement is when two particles link together in a certain way no matter how far apart they are in space.

翻译中…

You are given a string $s$. A pair of its non-empty substrings $(a, b)$ is called entangled if there is a (possibly empty) link string $c$ such that:

翻译中…
  • Every occurrence of $a$ in $s$ is immediately followed by $cb$;
  • Every occurrence of $b$ in $s$ is immediately preceded by $ac$.
翻译中…

In other words, $a$ and $b$ occur in $s$ only as substrings of $acb$. Compute the total number of entangled pairs of substrings of $s$.

翻译中…

A string $a$ is a substring of a string $b$ if $a$ can be obtained from $b$ by the deletion of several (possibly zero or all) characters from the beginning and several (possibly zero or all) characters from the end.

翻译中…

Input

翻译中…

The first and only line contains a string $s$ of lowercase English letters ($1 \leq |s| \leq 10^5$) — the string for which you should count pairs of entangled substrings.

翻译中…

Output

翻译中…

Output a single integer, the number of entangled pairs of substrings of $s$.

翻译中…

Note

翻译中…

In the first example, the only entangled pair is (ab,ba). For this pair, the corresponding link string $c$ is empty, as they only occur as substrings of the whole string abba, which doesn't have any characters between ab and ba.

翻译中…

In the second example, there are no entangled pairs.

翻译中…

In the third example, the entangled pairs are (a,b), (b,c), (a,c), (a,bc), and (ab,c). For most pairs, the corresponding link string $c$ is empty, except for the pair (a,c), for which the link string $c$ is b, as a and c only occur as substrings of the string abc.

翻译中…

样例

Input #1
abba
Output #1
1
InputCopy OutputCopy
Input #2
abacaba
Output #2
0
InputCopy OutputCopy
Input #3
abcabcabcabc
Output #3
5
InputCopy OutputCopy
Input #4
adamant
Output #4
82
InputCopy OutputCopy
题目信息
题号
1817F
时间限制
2 s
标签
后缀结构
字符串

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。