Radio Transmission
InterviewTime limit1sMemory limit128 MB
Given a received string that is part of a repeated broadcast, find the length of the shortest repeating unit using KMP failure function.
- Level
Medium4 of 10
- Topics
- String matching, String
- Solved
- No attempts yet
Problem
A radio station transmits a single message to many listeners. To make sure every listener receives it, the station broadcasts the same message over and over, concatenating it end to end without stopping.
You are given a string S that one listener received. The length of the received string is always greater than or equal to the length of the original message the station actually sent. Write a program that recovers the original message.
Formally, given a string S, find the shortest string S′ such that S is a substring of S′ + S′ + ⋯ + S′ (S′ repeated some number of times), and output the length of that S′.
Input
The first line contains the length L of S. The second line contains the string S of length L. S consists only of lowercase letters. (1 ≤ L ≤ 1,000,000)
Output
Print the length L′ of the shortest original message S′.
Hint
For example, if S is cabcabca, possible original messages include cab, abc, and abcabc, and no message shorter than length 3 exists. Therefore the answer is 3.