iliya785

Rabin-Karp

May 27th, 2012
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 0.51 KB | None | 0 0
  1. const
  2.    p = 31;
  3. var
  4.     h,pow:array[0..255] of longint;
  5.     s,t:string;
  6.     i,j,n,h_,h_s:longint;
  7.     e:boolean;
  8.  
  9. Begin
  10.   readln(t);
  11.   readln(s);
  12.   pow[1]:=1;
  13.   for i:=2 to length(t) do
  14.     pow[i]:=pow[i - 1] * p;
  15.   for i:=1 to length(t) do
  16.     h[i]:=(ord(t[i]) - 96) * pow[i] + h[i - 1];
  17.  for i:=1 to length(s) do
  18.    h_s:=h_s+(ord(s[i]) - 96) * pow[i];
  19.  for i:=1 to length(t) - length(s) +  1 do
  20.    begin
  21.      h_:=h[i+length(s) - 1] - h[i - 1];
  22.      if h_ = h_s * pow[i] then e:=true;
  23.    end;
  24. end.
Advertisement
Add Comment
Please, Sign In to add comment