KMP核心代码

来源:互联网 发布:libevent源码解析 编辑:程序博客网 时间:2024/06/13 12:15

计算预处理

P[1]:=0;j:=0;for i:=2 to m dobegin   while (j>0) and (B[j+1]<>B[i]) do j:=P[j];   if B[j+1]=B[i] then j:=j+1;   P[i]:=j;end;

KMP匹配

j:=0;for i:=1 to n dobegin   while (j>0) and (B[j+1]<>A[i]) do j:=P[j];   if B[j+1]=A[i] then j:=j+1;   if j=m then   begin      writeln('Pattern occurs with shift ',i-m);      j:=P[j];   end;end;

Matrix67大神的代码,好好研究一下

原创粉丝点击