Cod sursa(job #1105966)

Utilizator ctlin04UAIC.VlasCatalin ctlin04 Data 12 februarie 2014 12:14:49
Problema Hashuri Scor 100
Compilator fpc Status done
Runda Arhiva educationala Marime 1.62 kb
Program hashuri;
 const md=666013;
type lista=^celula;
     celula=record
            v:longint;
            next:lista;
            end;
var h:array [1..666013] of lista;
    i,x,op,n,x1:longint;
    b1,b2:array [1..1 shl 17] of char;
    ok:boolean;
    fi,fo:text;
procedure insert(x:longint);
 var r,p:lista;
      ok:boolean;
begin
 x1:=x mod md; r:=h[x1]; ok:=true;
 if r=nil then begin new(p); p^.v:=x; h[x1]:=p; end
 else begin
   while r<>nil do begin
                   if r^.v=x then ok:=false;
                    p:=r; r:=r^.next;
                   end;
       if ok then begin new(r); r^.v:=x; p^.next:=r; end;
       end;
end;
procedure erase(x:longint);
 var r,p:lista;
     l:integer;
begin
 x1:=x mod md; r:=h[x1]; p:=r; l:=0;
  while (r<>nil) and (r^.v<>x) do begin p:=r; r:=r^.next; inc(l); end;
  if (l=0) and (r<>nil) then begin h[x1]:=r^.next; dispose(r); end
  else if r<>nil then begin p:=r^.next; dispose(r); end;
end;
procedure cauta(x:longint);
 var r:lista;
      x1:longint;
begin
 ok:=false; x1:=x mod md; r:=h[x1];
  while r<>nil do begin
                  if r^.v=x then ok:=true;
                  r:=r^.next;
                  end;
end;
begin
 assign(fi,'hashuri.in');
  assign(fo,'hashuri.out');
 settextbuf(fi,b1); settextbuf(fo,b2);
 reset(fi); rewrite(fo); readln(fi,n);
  for i:=1 to n do begin
                    readln(fi,op,x);
                    if op=1 then insert(x)
                    else if op=2 then erase(x)
                     else begin cauta(x); if ok then writeln(fo,'1') else writeln(fo,'0'); end;
                    end;
  close(fo);
end.