bzoj 1355 kmp

题意:给一个字符串,求最小循环节(可以不整除)

裸kmp,next数组的性质

 i % ( i - next[i] ) == 0 && next[i] != 0 , 则说明字符串循环,而且

循环节长度为:   i - next[i]

循环次数为:       i / ( i - next[i] )

因为不要求整除所以ans=n-next[n]

var
        n               :longint;
        s               :ansistring;
        next            :array[0..1000010] of longint;
procedure kmp;
var
        p:longint;
        i:longint; 
begin
   p:=0;
   for i:=2 to n do
   begin
      while (p<>0) and (s[i]<>s[p+1]) do p:=next[p];
      if s[i]=s[p+1] then
      begin
         inc(p);
         next[i]:=p;
      end else next[i]:=0;
   end;
end;

begin
   readln(n);
   readln(s);
   kmp;
   writeln(n-next[n]);
end.
——by Eirlys

bzoj 1355 kmp_第1张图片

你可能感兴趣的:(bzoj,kmp&extend-kmp)