Cod sursa(job #352769)

Utilizator ionutz32Ilie Ionut ionutz32 Data 3 octombrie 2009 14:02:33
Problema Trie Scor 100
Compilator fpc Status done
Runda Arhiva educationala Marime 2.14 kb
type ref=^nod;
nod=record
    fii,nrc:longint;
    v:array['a'..'z'] of ref;
    end;
var rad:ref;
s:string[28];
i:longint;
f,g:text;
procedure fillc(nod:ref);
          var j:longint;
          begin
          nod^.fii:=0;
          nod^.nrc:=0;
          for j:=97 to 122 do
              nod^.v[chr(j)]:=nil;
          end;
procedure add(nod:ref;poz:longint);
          begin
          if poz=length(s)+1 then
             inc(nod^.nrc)
          else
              begin
              if nod^.v[s[poz]]=nil then
                 begin
                 new(nod^.v[s[poz]]);
                 inc(nod^.fii);
                 fillc(nod^.v[s[poz]]);
                 end;
              add(nod^.v[s[poz]],poz+1);
              end;
          end;
procedure delete(nod:ref;poz:longint);
          begin
          if poz=length(s)+1 then
             dec(nod^.nrc)
          else
              begin
              delete(nod^.v[s[poz]],poz+1);
              if (nod^.v[s[poz]]^.nrc=0) and (nod^.v[s[poz]]^.fii=0) then
                 begin
                 dispose(nod^.v[s[poz]]);
                 nod^.v[s[poz]]:=nil;
                 dec(nod^.fii);
                 end;
              end;
          end;
function nr(nod:ref;poz:longint):longint;
         begin
         if poz=length(s)+1 then
            nr:=nod^.nrc
         else
             if nod^.v[s[poz]]=nil then
                nr:=0
             else
                 nr:=nr(nod^.v[s[poz]],poz+1);
         end;
function max(nod:ref;poz:longint):longint;
         begin
         if poz=length(s)+1 then
            max:=length(s)-2
         else
             if nod^.v[s[poz]]<>nil then
                max:=max(nod^.v[s[poz]],poz+1)
             else
                 max:=poz-3;
         end;
begin
assign(f,'trie.in');
assign(g,'trie.out');
reset(f);rewrite(g);
new(rad);
fillc(rad);
while not eof(f) do
      begin
      readln(f,s);
      case s[1] of
           '0':add(rad,3);
           '1':delete(rad,3);
           '2':writeln(g,nr(rad,3));
           '3':writeln(g,max(rad,3));
           end;
      end;
close(f);close(g);
end.