Cod sursa(job #411678)

Utilizator saodem74hieu tran saodem74 Data 5 martie 2010 03:13:54
Problema Trie Scor 45
Compilator fpc Status done
Runda Arhiva educationala Marime 1.51 kb
uses  math;
const tfi='trie.in';
      tfo='trie.out';
      maxn=100100;
type  nut=record
            c:array['a'..'z'] of longint;
            cc,vt:longint;
          end;
var       fi,fo:text;
          cnt:longint;
          a:array[0..maxn] of nut;

procedure add(ss:string);
var i,j:Longint;
begin
  i:=1;
  for j:=1 to length(ss) do
   begin
    if a[i].c[ss[j]]=0 then
     begin
      inc(cnt);
      a[i].c[ss[j]]:=cnt;
     end;
    i:=a[i].c[ss[j]];
    inc(a[i].vt);
   end;
  inc(a[i].cc);
end;

function find(ss:string):longint;
var i,j:longint;
begin
  i:=1;
  for j:=1 to length(ss) do i:=a[i].c[ss[j]];
  exit(a[i].cc);
end;


function find3(ss:string):longint;
var tg,h,i,j:longint;
begin
  i:=1;h:=0; tg:=0;
  for j:=1 to length(ss) do
   begin
    i:=a[i].c[ss[j]];
    if (i=0) or (a[i].vt=0) then break;
    inc(h);
    tg:=max(tg,h);
   end;
  exit(tg);
end;

procedure del(ss:string);
var i,j:longint;
begin
  i:=1;
  for j:=1 to length(ss) do
   begin
    i:=a[i].c[ss[j]];
    dec(a[i].vt);
   end;
  dec(a[i].cc);
end;

procedure process;
var i,j:longint;
  ch:char;
  s:string;
begin
  cnt:=1;
  while not seekeof(fi) do
   begin
    read(Fi,i,ch);
    readln(fi,s);
    if i=0 then add(s) else
    if i=1 then del(s)  else
    if i=2 then writeln(fo,find(s)) else
    if i=3 then writeln(fo,find3(s)) else
   end;
end;

begin
  assign(fi,tfi); reset(fi);
  assign(Fo,tfo); rewrite(fo);
  process;
  close(Fi); close(fo);
end.