Cod sursa(job #119813)

Utilizator eugen.nodeaEugen Nodea eugen.nodea Data 3 ianuarie 2008 13:14:46
Problema Ordine Scor 0
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.5 kb
const fi='ordine.in';
      fo='ordine.out';
var
   f:text;
   A:array[1..1000] of char;
   Ap:Array['a'..'z'] of longint;
   p,i,u,k:longint;
   c,ch:char;
Begin
     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);
     c:='a';
     while Ap[c]=0 do
           c:=succ(c);
     u:=0;
     while u<k do
     begin
          u:=u+1;
          ch:=succ(c);
          while (ch<='z') and (Ap[ch]<Trunc((k-u+1)/2)+1) do
                ch:=succ(ch);
          if ch in ['a'..'z'] then
             begin
                  A[u]:=ch;
                  Ap[ch]:=Ap[ch]-1;
             end
             else begin
                       while Ap[c]=0 do
                             c:=succ(c);
                       if c=A[u-1] then begin
                                   ch:=succ(c);
                                   while (Ap[ch]=0) and (ch<'z')do
                                         ch:=succ(ch);
                                   A[u]:=ch;
                                   Ap[ch]:=Ap[ch]-1;
                                   end
                       else begin
                                 A[u]:=c;
                                 Ap[c]:=Ap[c]-1;
                            end;
             end;
     end;
     assign(f,fo);rewrite(f);
     i:=1;
     while A[i]<>#0 do begin
         write(f,A[i]);
         i:=i+1;
         end;
     close(f);
End.