Cod sursa(job #290232)

Utilizator FllorynMitu Florin Danut Flloryn Data 27 martie 2009 17:33:42
Problema Ordine Scor 100
Compilator fpc Status done
Runda Arhiva de probleme Marime 0.91 kb
program pascal;
var f,g:Text;  ta:ansistring; q,t,aux:char; v:array[97..122] of longint;
    s:array[0..1000000] of char;
    i,j,n:longint;
 procedure citire;
 begin
 assign(f,'ordine.in'); reset(f);
 assign(g,'ordine.out'); rewrite(g);
 read(f,ta);
 n:=length(ta);
 for i:=1 to n do inc(v[ord(ta[i])]);
 close(f);
 end;

 procedure construire;
 begin
 s[0]:='1';
 for i:=1 to n do
   begin
   aux:='1';  t:='z';
   for j:=97 to 122 do
   if v[j]<>0 then
     begin
     q:=chr(j);
     if (v[j]>=(n-i+1) div 2+1) and (s[i-1]<>q) then aux:=q
     else
     if (q<t) and (s[i-1]<>q) then t:=q;
     end;

   if aux='1' then
        begin
        write(g,t);
        dec(v[ord(t)]);
        s[i]:=t;
        end
       else begin
            write(g,aux);
            dec(v[ord(aux)]);
            s[i]:=aux;
            end;
   end;
   close(g);
 end;

begin
citire;
construire;
end.