{ Starting Number : 12 }
{ Solved tasks : 1,2,(3) }

uses crt,dos;
var txt1,txt2:text;
    dirinfo:searchrec;
    tabledescription:string[12];
    formattedtable:string[12];
    s:string;
    n,code,i,ii,i3,i4,i5:integer;
    x,xx,lnb:byte;
    l,ww,direct:string;
    tab:array[1..256,1..99]of char;
    wtab:array[1..8,1..49]of string[90];
    lh:array[1..9]of byte;
    wctab:array[1..8,1..49]of byte;
procedure help;
 begin
 end;
function optim(ss:string):string;
var iii:integer;
 begin
  iii:=1;
  while ss[length(ss)]=' ' do delete(ss,length(ss),1);
  repeat
   while (ss[iii]<>' ')and(iii<length(ss)) do inc(iii);
   if iii<length(ss) then
    begin
     while ss[iii+1]=' ' do delete(ss,iii,1);
     inc(iii);
    end;
  until iii=length(ss);
  if ss[length(ss)]=' ' then delete(ss,length(ss),1);
  iii:=1;
  while (ss[iii]<>'&')and(iii<length(ss)) do inc(iii);
  if ss[iii]='&' then
   begin
    if ss[iii+1]='l' then
     begin
      delete(ss,iii,3);
      insert('<',ss,iii);
     end;
    if ss[iii+1]='g' then
     begin
      delete(ss,iii,3);
      insert('>',ss,iii);
     end;
    if ss[iii+1]='a' then
     begin
      delete(ss,iii,4);
      insert('&',ss,iii);
     end;
   end;
  optim:=ss;
 end;
begin
 getdir(0,direct);
 chdir('c:\icp');
 if paramcount<2 then
  begin
   writeln('Too few parameters !!!');
   help;
   chdir(direct);
   halt(1);
  end;
 if paramcount>3 then
  begin
   writeln('Too many parameters !!!');
   help;
   chdir(direct);
   halt(1);
  end;
 if paramcount=3 then
  begin
   s:=paramstr(1);
   delete(s,1,1);
   val(s,n,code);
   if code<>0 then
    begin
     writeln('Invalid length of line !!!');
     help;
     chdir(direct);
     halt(1);
    end;
   tabledescription:=paramstr(2);
   formattedtable:=paramstr(3);
  end
 else
  begin
   n:=80;
   tabledescription:=paramstr(1);
   formattedtable:=paramstr(2);
  end;
 findfirst(tabledescription,anyfile,dirinfo);
 if doserror<>0 then
  begin
   writeln('Table description file not found !!!');
   help;
   chdir(direct);
   halt(1);
  end;
 for i:=1 to 256 do for ii:=1 to 99 do tab[i,ii]:=' ';
 for i:=1 to 8 do for ii:=1 to 49 do wtab[i,ii]:=' ';
 assign(txt1,tabledescription);
 reset(txt1);
 lnb:=0;
 x:=0;
 repeat
  readln(txt1,l);
  repeat
   if copy(l,1,4)='<TR>' then
    begin
     inc(lnb);
     xx:=0;
     delete(l,1,4);
     while l[1]=' ' do delete(l,1,1);
    end;
   if copy(l,1,3)='<TH' then
    begin
     inc(x);
{     while wctab[x,lnb]=3 do inc(x);}
     delete(l,1,3);
     wctab[x,lnb]:=0;
     if l[1]=' ' then
      if l[2]='A' then
       begin
        delete(l,1,7);
        if upcase(l[1])='L' then wctab[x,lnb]:=0 else
        if upcase(l[1])='C' then wctab[x,lnb]:=1 else
        if upcase(l[1])='R' then wctab[x,lnb]:=2;
        while l[1]<>'>' do delete(l,1,1);
       end
      else
       begin
        delete(l,1,1);
        if upcase(l[1])='C' then wctab[x,lnb]:=3 else
        if upcase(l[1])='R' then wctab[x,lnb]:=4;
        while l[1]<>'=' do delete(l,1,1);
        delete(l,1,1);
        i3:=1;
        while l[i3]<>'>' do inc(i3);
        val(copy(l,1,i3-1),i4,code);
        while l[1]<>'>' do delete(l,1,1);
        if wctab[x,lnb]=3 then
         begin
          dec(x);
          for i3:=1 to i4-1 do wctab[x+i3-1,lnb]:=3;
         end;
        if wctab[x,lnb]=4 then
         for i3:=1 to i4-1 do wctab[x,lnb+i3-1]:=4;
       end;
     delete(l,1,1);
     i:=1;
     while (l[i]<>'<')and(i<=length(l)) do inc(i);
     wtab[x,lnb]:=optim(copy(l,1,i-1));
     delete(l,1,i-1);
     if wctab[x,lnb]=3 then
      begin
       i3:=0;
       while wctab[x+i3,lnb]=3 do begin;wtab[x+i3,lnb]:=wtab[x,lnb];inc(i3);end;
      end;
     if wctab[x,lnb]=4 then
      begin
       i3:=0;
       while wctab[x,lnb+i3]=4 do begin;wtab[x,lnb+i3]:=wtab[x,lnb];inc(i3);end;
      end;
    end;
   if copy(l,1,3)='<TD' then
    begin
     inc(xx);
{     while wctab[xx,lnb]=3 do inc(xx);}
     if xx>x then x:=xx;
     delete(l,1,3);
     wctab[xx,lnb]:=0;
     if l[1]=' ' then
      if l[2]='A' then
       begin
        delete(l,1,7);
        if upcase(l[1])='L' then wctab[xx,lnb]:=0 else
        if upcase(l[1])='C' then wctab[xx,lnb]:=1 else
        if upcase(l[1])='R' then wctab[xx,lnb]:=2;
        while l[1]<>'>' do delete(l,1,1);
       end
      else
       begin
        delete(l,1,1);
        if upcase(l[1])='C' then wctab[xx,lnb]:=3 else
        if upcase(l[1])='R' then wctab[xx,lnb]:=4;
        while l[1]<>'=' do delete(l,1,1);
        delete(l,1,1);
        i3:=1;
        while l[i3]<>'>' do inc(i3);
        val(copy(l,1,i3-1),i4,code);
        while l[1]<>'>' do delete(l,1,1);
        if wctab[xx,lnb]=3 then
         begin
          dec(x);
          for i3:=1 to i4-1 do wctab[xx+i3-1,lnb]:=3;
         end;
        if wctab[x,lnb]=4 then
         for i3:=1 to i4-1 do wctab[xx,lnb+i3-1]:=4;
       end;
     delete(l,1,1);
     i:=1;
     while (l[i]<>'<')and(i<=length(l)) do inc(i);
     wtab[xx,lnb]:=optim(copy(l,1,i-1));
     delete(l,1,i-1);
     if wctab[xx,lnb]=3 then
      begin
       i3:=0;
       while wctab[xx+i3,lnb]=3 do begin;wtab[xx+i3,lnb]:=wtab[xx,lnb];inc(i3);end;
      end;
     if wctab[xx,lnb]=4 then
      begin
       i3:=0;
       while wctab[xx,lnb+i3]=4 do begin;wtab[xx,lnb+i3]:=wtab[xx,lnb];inc(i3);end;
      end;
    end;
  until l='';
 until eof(txt1);
 close(txt1);
 for i:=1 to x do
  begin
   xx:=0;
   for ii:=1 to lnb do
    if length(wtab[i,ii])>xx then xx:=length(wtab[i,ii]);
   lh[i]:=xx;
  end;
 lh[9]:=0;
 for i:=1 to x do lh[9]:=lh[9]+lh[i];
 lh[9]:=lh[9]+(3*x)+1;
 if lh[9]>n then
  begin
   writeln('Error: Table is too long to fit line.');
   chdir(direct);
   halt(1);
  end;

 for i:=1 to x do for ii:=1 to lnb do
  begin
   ww:='';
   for i3:=1 to (lh[i]-length(wtab[i,ii])) do insert(' ',ww,1);
   if wctab[i,ii]=0 then insert(ww,wtab[i,ii],length(wtab[i,ii])+1);
   if wctab[i,ii]=2 then insert(ww,wtab[i,ii],1);
   i3:=length(ww) div 2;
   ww:='';
   for i4:=1 to i3 do insert(' ',ww,1);
   if wctab[i,ii]=1 then
    begin
     insert(ww,wtab[i,ii],1);
     insert(ww+' ',wtab[i,ii],length(wtab[i,ii])+1);
    end;
   if wctab[i,ii]=3 then
    begin
     i3:=0;
     while wtab[i+i3,ii]=wtab[i,ii] do inc(i3);
     i4:=0;
     for i5:=1 to i3 do i4:=i4+(lh[i+i5-1])+3;
     i4:=i4-3;
     i4:=i4 div 2;
     ww:='';
     for i5:=1 to i4 do insert(' ',ww,1);
     insert(ww,wtab[i,ii],1);
     insert(ww+' ',wtab[i,ii],length(wtab[i,ii])+1);
    end;
   if wctab[i,ii]=4 then
    begin
     i3:=(lh[i]-length(wtab[i,ii]))div 2;
     ww:='';
     for i4:=1 to i3 do insert(' ',ww,1);
     i3:=0;
     insert(ww,wtab[i,ii],1);
     insert(ww+' ',wtab[i,ii],length(wtab[i,ii])+1);
     i4:=0;
     while wtab[i,ii+i4]=wtab[i,ii] do inc(i4);
     i3:=lnb*2+i4-1;
     wctab[i,ii+1]:=i3;
    end;
  end;

 for i:=1 to lh[9] do for ii:=1 to (lnb*2+1) do
  begin
   if (ii mod 2)=1 then
    begin
     tab[i,ii]:='-';
     if (i=1)or(i=lh[9]) then tab[i,ii]:='+';
     i3:=i-1;
     i4:=1;
     while i3>(lh[i4]+3) do
      begin
       i3:=i3-lh[i4]-3;
       inc(i4);
      end;
     if i3=lh[i4]+3 then tab[i,ii]:='+';
    end;
   if (ii mod 2)=0 then if (i=1)or(i=lh[9]) then tab[i,ii]:='|'
    else
     begin
      i3:=i-1;
      i4:=1;
      while i3>(lh[i4]+3) do
       begin
        i3:=i3-lh[i4]-3;
        inc(i4);
       end;
      if i3=lh[i4]+3 then tab[i,ii]:='|' else
      if (i3=lh[i4]+2)or(i3=1) then tab[i,ii]:=' ' else
       tab[i,ii]:=wtab[i4,ii div 2][i3-1];
     end;
  end;
 for i:=1 to x do for ii:=1 to lnb do
  begin
   if wctab[i,ii]=3 then
    begin
     i3:=0;
     for i4:=1 to x-1 do i3:=i3+lh[i4]+3;
     i3:=i3+2;
     tab[i3,ii*2+1]:=' ';
     for i4:=1 to length(wtab[i,ii]) do tab[i3+i4,ii*2+1]:=wtab[i,ii][i4];
     if tab[i3+length(wtab[i,ii])+1,ii*2+1]<>'|' then
      tab[i3+length(wtab[i,ii])+1,ii*2+1]:=' ';

    end;
   if wctab[i,ii]=4 then
    begin
     i3:=wctab[i,ii+1];
     for i4:=1 to length(wtab[i,ii]) do tab[i4+2,ii+i3]:=wtab[i,ii][i4];
    end;
  end;

 clrscr;
 for i:=1 to lh[9] do for ii:=1 to (lnb*2+1) do
  begin
   gotoxy(i,ii);
   write(tab[i,ii]);
  end;

 assign(txt1,formattedtable);
 rewrite(txt1);
 for i:=1 to lh[9] do
  begin
   for ii:=1 to lnb*2+1 do write(txt1,tab[i,ii]);
   writeln(txt1,'');
  end;
 close(txt1);
 chdir(direct);
end.