algorithm - Find the smallest period of input string in O(n)? -
given following problem :
definition :
let s string on alphabet Σ .
s'smallest period ofsifs'smallest string such :
s = (s')^k (s''),where
s''prefix ofs. if no suchs'exists ,snot periodic .example :
s = abcabcabcabca.abcabcperiod sinces = abcabc abcabc a, smallest periodabcsinces = abc abc abc abc a.give algorithm find smallest period of input string
sor declaresnot periodic.hint : can in
o(n)...
my solution : use kmp , runs in o(n) .
by definition of problem , s = (s')^k (s'') , think if create automata shortest period , , find way find shortest period , i'm done.
the problem put fail arrow of automata ...
any ideas appreciated ,
regards
i'm not sure understand attempted solution. kmp useful subroutine, though -- smallest period how far kmp moves needle string (i.e., s) after complete match.
Comments
Post a Comment