Given the following problem :
Definition :
Let S be a string over alphabet Σ .
S'
is the smallest period ofS
ifS'
is the smallest string such that :
S = (S')^k (S'')
,where
S''
is a prefix ofS
. If no suchS'
exists , thenS
is not periodic .Example :
S = abcabcabcabca
. Thenabcabc
is a period sinceS = abcabc abcabc a
, but the smallest period isabc
sinceS = abc abc abc abc a
.Give an algorithm to find the smallest period of input string
S
or declare thatS
is not periodic.Hint : You can do that in
O(n)
...
My solution : We use KMP , which runs in O(n) .
By the definition of the problem , S = (S')^k (S'') , then I think that if we create an automata for the shortest period , and find a way to find that shortest period , then I'm done.
The problem is where to put the FAIL arrow of the automata ...
Any ideas would be greatly appreciated ,
Regards
I'm not sure that I understand your attempted solution. KMP is a useful subroutine, though -- the smallest period is how far KMP moves the needle string (i.e.,
S
) after a complete match.