Prefixuffix
Time limit3sMemory limit512 MB
Given a string t, find the maximum length L, at most n/2, such that the length-L prefix and the length-L suffix of t are cyclic rotations of each other.
- Level
Hard8 of 10
- Topics
- String, String matching, Hash map, Prefix sum
- Solved
- No attempts yet
Problem
In this problem we only consider strings made of lowercase letters of the English alphabet. An initial fragment of a string is called a prefix, and a final fragment is called a suffix. In particular, the empty string is both a prefix and a suffix of every string.
Two strings are cyclically equivalent when one can be turned into the other by taking some suffix of it and moving that suffix from the end to the front. For example, ababba and abbaab are cyclically equivalent, whereas ababba and ababab are not. Every string is cyclically equivalent to itself.
You are given a string of length . Find a prefix and a suffix of , both of the same length, such that:
- and are cyclically equivalent,
- their common length is at most (so that and do not overlap in ), and
- their common length is as large as possible.
Input
The first line contains one integer , the length of the string . ()
The second line contains the string itself, consisting of lowercase letters of the English alphabet.
Output
Print one integer: the largest common length of a prefix and a suffix that satisfy the conditions above.