This page is still under construction.

Parts of this page are still being built. What you see may change.

Prefix-Suffixes

Time limit1sMemory limit128 MB

Summary
Count proper borders summed over all substrings of a given lowercase word of length up to 10^5.
Level

Hard9 of 10

Topics
String matching, String, Prefix sum
Solved
No attempts yet

Problem

A prefix-suffix (a border) of a word ww is a word vv that is both a prefix (an initial fragment) and a suffix (a final fragment) of ww. A proper prefix-suffix of ww is any prefix-suffix that is non-empty and strictly shorter than ww. Let PS(w)\mathrm{PS}(w) denote the number of proper prefix-suffixes of ww. Let w[i,j]w[i, j] denote the substring of ww that starts at position ii and ends at position jj; positions are numbered from 11.

Given a word ww, compute the total number of proper prefix-suffixes over all substrings of ww, that is

∑1≤i≤j≤∣w∣PS(w[i,j])\sum_{1 \le i \le j \le |w|} \mathrm{PS}(w[i, j])

Input

The first and only line of input contains the word ww. Its length satisfies 1≤∣w∣≤1051 \le |w| \le 10^5, and it consists only of lowercase English letters.

Output

Print a single integer: the total number of proper prefix-suffixes over all substrings of ww.

Examples4

  1. Example 1

    Input
    ababa
    
    Expected output
    7
    
  2. Example 2

    Input
    aa
    
    Expected output
    1
    
  3. Example 3

    Input
    aaaa
    
    Expected output
    10
    
  4. Example 4

    Input
    abab
    
    Expected output
    3