ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1067. Disk Tree

Why Crash???
Posted by Cosine 3 Aug 2003 08:20
type point=^node;
     node=record
       data:string;
       last:integer;
       child:array [1..100] of point;
     end;

var head,headq,q:point;
    i,j,k,n,lasto:integer;
    s,v:string;
    visited:boolean;
    order:array [1..5000] of integer;

procedure init;
  var i:char;
begin
  lasto:=0;
  new(head); head^.last:=0; head^.data:='';
end;

procedure add(p:point);
  var i:integer;
      same:boolean;
begin
  if p^.data=headq^.data then begin

    if headq^.last<>0 then begin

      same:=false;

      for i:=1 to p^.last do
        if p^.child[i]^.data=headq^.child[1]^.data then begin
          same:=true;
          break;
        end;

      if same then begin
        headq:=headq^.child[1];
        add(p^.child[i]);
      end
      else begin
        p^.last:=p^.last+1;
        p^.child[p^.last]:=headq^.child[1];
      end;

    end;

    visited:=true;

  end;
  for i:=1 to p^.last do add(p^.child[i]);
end;

procedure print(p:point; k:integer);
  var i,j:integer;
        s:point;
begin

  for i:=1 to k do write(' ');

  for i:=1 to p^.last-1 do
    for j:=i+1 to p^.last do
      if p^.child[i]^.data>p^.child[j]^.data then begin
        s:=p^.child[i];
        p^.child[i]:=p^.child[j];
        p^.child[j]:=s;
      end;

  if p^.data<>'' then writeln(p^.data);

  for i:=1 to p^.last do print(p^.child[i],k+1);
end;

begin

  init;
  readln(n);

  for i:=1 to n do begin

    readln(s); v:='';
    new(headq); headq^.last:=0; q:=headq;

    for j:=1 to length(s) do begin
      if s[j]<>'\' then v:=v+s[j]
         else begin
           q^.data:=v;
           q^.last:=1;
           new(q^.child[1]); q:=q^.child[1];
           v:='';
         end;
    end;

    q^.last:=0;  q^.data:=v;
    visited:=false;
    add(head);

    if not visited then begin

       head^.last:=head^.last+1;
       head^.child[head^.last]:=headq;
       k:=0;

       for j:=1 to lasto do
         if head^.child[order[j]]^.data>headq^.data then begin
           for k:=lasto+1 downto j+1 do order[k]:=order[k-1];
           order[j]:=head^.last;
           break;
         end;

       lasto:=lasto+1;
       if lasto=1 then order[lasto]:=head^.last;

    end;
  end;

  print(head,-1);

end.
Re: Why Crash???
Posted by vladu adrian 21 Aug 2003 01:41
You didn't manage well those chained lists, there are points who do
not have a child, nor a reference to nil