字符串hash模板【pascal】

const
        base=137;
        step=13;
        mo=1000000;
var
        n,ans,p,l       :longint;
        s               :array[0..10010] of ansistring;
        heap            :array[0..1000010] of longint;
        i,j             :longint;
begin
   readln(n);ans:=0;
   for i:=1 to n do
   begin
      readln(s[i]);
      p:=0;l:=length(s[i]);
      for j:=1 to l do p:=(p+ord(s[i][j])*base) mod mo;
      while (heap[p]<>0) do
      begin
         if s[heap[p]]=s[i] then break;
         inc(p,step);
      end;
      if (heap[p]=0) then
      begin
         inc(ans);
         heap[p]:=i;
      end;
   end;
   writeln(ans);
end.
——by Eirlys

你可能感兴趣的:(Hash,模板)