Cod sursa(job #578733)

Utilizator andreifirstCioara Andrei Ioan andreifirst Data 11 aprilie 2011 15:43:46
Problema Trie Scor 0
Compilator fpc Status done
Runda Arhiva educationala Marime 1.97 kb
type muchie=^nod;
     nod = record n, f:longint; v:array [1..26] of muchie; end;

var p, r, st:muchie;
    buf1, buf2:array [1..1 shl 17] of char;
    i, j, m, c, o:longint;
    ch:char;
    f, g:text;
    ok:boolean;
    s:string;

procedure del(a:muchie);
var oo:longint;
  begin
  dec (a^.f);
  if s[i] <> ' ' then
    begin
    oo:=ord(s[i])-96;
    inc (i);
    del(a^.v[oo]);
    if a^.v[oo]^.f=0 then a^.v[oo]:=nil
    end
                  else
    begin
    dec(a^.n);
    end;
  end;

begin
assign (f, 'trie.in'); settextbuf (f, buf1); reset (f);
assign (g, 'trie.out'); settextbuf (g, buf2); rewrite (g);

new(st); st^.n:=1; for j := 1 to 26 do st^.v[j]:=nil;
while not eof (f) do
  begin
  read (f, c); read (f, ch); readln (f, s); s:=s+' ';
  case c of
    0:begin
      p:=st;
      inc(p^.f);
      i:=1;
      o:=ord(s[i])-96;
      while (p^.v[o]<>nil) and (s[i]<> ' ') do
        begin
        p:=p^.v[o];
        inc(i);
        if s[i]<> ' ' then o:=ord(s[i])-96;
        if p^.v[o]<> nil then inc (p^.f);
        end;
      while s[i] <> ' ' do
        begin
        o:=ord(s[i])-96;
        new (r);
        for j := 1 to 26 do r^.v[j]:= nil;
        r^.n :=0; r^.f :=0;
        inc (p^.f);
        p^.v[o]:=r; p:=r;
        inc(i);
        end;
      inc (p^.n); inc(p^.f);
      end;
    1:begin
      i:=1; ok:=false;
      del(st);
      end;
    2:begin
      p:=st;
      i:=1;
      while s[i] <> ' ' do
        begin
        o:=ord(s[i])-96;
        p:=p^.v[o];
        inc (i);
        end;
        writeln (g, p^.n);
      end;
    3:begin
      p:=st;
      i:=1;
      if p <> nil then
        begin
        while (p<> nil) and (s[i]<> ' ') do
          begin
          o:=ord(s[i])-96;
          p:=p^.v[o];
          inc (i);
          end;
        dec (i);
        end;
      if p = nil then dec (i);
      writeln (g, i);
      end;
    end;
  end;

close (f); close (g);
end.