Prefixes and Suffixes
HardRating 2000CodeforcesString Algorithms (KMP, Z, Hashing)
Approach hintTry it first, then revealHide
Borders come from walking the prefix-function chain from pi[n-1]. For occurrence counts, tally every pi value, then push counts down the chain from longer prefixes to shorter ones, adding one for each prefix itself.