#Q48. 「一本通 2.2 练习 2」OKR-Periods of Words
「一本通 2.2 练习 2」OKR-Periods of Words
Description
Original source: POI 2006
A string is a finite sequence of lowercase letters. Specifically, an empty sequence can also be considered a string. A string is a prefix of string if and only if there exists a string such that . If and is not an empty string, then we say is a proper prefix of .
We define as a period of if and only if is a proper prefix of and is a prefix of (not necessarily a proper prefix). For example, the strings abab and ababab are both periods of the string abababa. The maximum period of string is its longest period or an empty string (when has no periods). For instance, the maximum period of ababab is abab, and the maximum period of abc is an empty string.
Given a string, calculate the sum of the lengths of the maximum periods for all its prefixes.
Input Format
The first line contains an integer , the length of the string.
The next line contains the given string.
Output Format
Output an integer representing the sum of the lengths of the maximum periods for all prefixes of the string.
Sample 1
8
babababa
24
Data Range and Hint
For all data, .