kmp模板,线性完成pos

var
 p:array[1..1000000] of longint;
 i,j:longint;
 a,b:ansistring;
 
  begin
 
    readln(a);
    readln(b);
 
    P[1]:=0;
    j:=0;
   for i:=2 to length(b) do
     begin
       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;
 
 
     j:=0;
    for i:=1 to length(a) do
        begin
          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=length(b) then
          begin
            writeln(i-length(b)+1);
            halt;
            j:=P[j];
         end;
        end;
     writeln(0);
   end.

 

posted @ 2016-02-24 14:04  z1j1n1  阅读(181)  评论(0编辑  收藏  举报