kmp用来快速求一个字符串的给定字串的数量。可以说是pascal的pos(),十分高效
详细解释点这里
求next
for i:=2 to m do
begin
while (j>0)and(s[i]<>s[j+1]) do j:=next[j];
if s[i]=s[j+1] then inc(j);
next[i]:=j;
end;
求KMP
function kmp(s1,s2:ansistring):longint;
var
i,j,k,n,ans:longint;
begin
j:=0;n:=length(s1);ans:=0;
for i:=1 to n do
begin
while (j>0)and(s1[i]<>s2[j+1]) do begin j:=next[j];end;
if s1[i]=s2[j+1] then inc(j);
if j=m then
begin
inc(ans);
j:=next[j];
end;
end;
exit(ans);
end;