The Shortest Period
Time limit5sMemory limit128 MB
Delete exactly one letter from a string to minimize the length of the shortest period of the resulting word.
- Level
Hard8 of 10
- Topics
- String, String matching, Brute force, Implementation
- Solved
- No attempts yet
Problem
We call a string a period of a string if and is a prefix of (the string written times in a row) for some positive integer . For example, the periods of the word entente are ent, entent, and entente, of lengths , , and . The shortest period of is its period of smallest length.
A teacher wrote a very long word on the whiteboard. Bytie was not paying much attention to the lesson, so instead he copied into his notebook every word that can be obtained from the whiteboard word by removing exactly one letter. If the whiteboard word has length , the notebook then holds words, each of length . Bytie now wants to pick the notebook word whose shortest period is as short as possible. Given the whiteboard word, report the length of that shortest possible shortest period.
Input
The first line contains one integer (), the number of test cases. Each of the next lines describes one test case: an integer (), the length of the whiteboard word, followed by a single space and the -letter word itself, made of lowercase English letters.
Output
Print lines. The -th line contains one integer: the smallest possible shortest period, taken over all words obtainable from the -th whiteboard word by deleting exactly one letter.
Note
Consider the word ababcaba. The words Bytie writes in his notebook, together with the length of each one's shortest period, are:
Removing the letter c in the fifth position gives abababa, whose shortest period has length , and no other single removal yields a shorter shortest period. Hence the answer is .