A Horrible Poem
Time limit8sMemory limit128 MB
Given a string and substring queries, find the length of the shortest full period of each substring, where a full period divides the substring into equal repeats.
- Level
Hard8 of 10
- Topics
- String, Number theory, Hash map, String matching
- Solved
- No attempts yet
Problem
Bytie has to memorize a fragment of a certain poem. The poem, in the spirit of modern art, is a long string made up only of lowercase English letters. It sounds horrible, but that is the least of Bytie's worries: he has completely forgotten which fragment he is supposed to learn, and every fragment looks hard to memorize.
There is hope, however, because some parts of the poem are regular. Every now and then a fragment is simply another fragment repeated several times, that is, for some integer . In that case we say that is a full period of (in particular, every string is a full period of itself). A fragment with a short full period is easy to memorize.
Given the whole poem and the list of fragments Bytie suspects, determine, for each fragment, the length of its shortest full period.
Input
The first line contains an integer (). The second line contains a string of length consisting of lowercase English letters, the poem; its characters are numbered from to .
The next line contains an integer (), the number of fragments. Each of the next lines contains two integers and (), separated by a single space, describing the fragment that starts at position and ends at position .
Output
Print lines. The -th line should contain a single integer: the length of the shortest full period of the -th fragment.