Cod sursa(job #120135)

Utilizator eugen.nodeaEugen Nodea eugen.nodea Data 4 ianuarie 2008 12:45:03
Problema Ordine Scor 0
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.78 kb
const fi='ordine.in';
      fo='ordine.out';
var
   f:text;
   Ap:Array['a'..'z'] of longint;
   u,k:longint;
   uc,c,ch:char;
Begin
     for c:='a' to 'z' do
         Ap[c]:=0;
    assign(f,fi);reset(f);
    k:=0;
    while Not eof(f) do
     begin
          read(f,c);
          k:=k+1;
          Ap[c]:=Ap[c]+1;
     end;
     close(f);
     assign(f,fo);rewrite(f);
     ch:='a';
     while Ap[ch]=0 do
          ch:=succ(ch);
     u:=0; uc:=#0;
     while u<k do
     begin
         u:=u+1;
         c:=ch;
         while (Ap[c]<Trunc(k-u+1)/2+1) and (c<='z') do
               c:=Succ(c);
         if (c<='z') then begin
                               uc:=c;
                               Ap[c]:=Ap[c]-1;
                               write(f,c);
                          end
                     else begin
                               if (c<>uc) then begin
                                                    uc:=c;
                                                    Ap[c]:=Ap[c]-1;
                                                    write(f,c);
                                               end
                                           else begin
                                                     c:=succ(c);
                                                     while (Ap[c]=0) and (c<='z') and (c<>uc) do
                                                           c:=succ(c);
                                                    uc:=c;
                                                    Ap[c]:=Ap[c]-1;
                                                    write(f,c);
                                                end;
                          end;
       ch:='a';
       while Ap[ch]=0 do
          ch:=succ(ch);
     end;
     close(f);
End.