Pagini recente » Cod sursa (job #1913749) | Cod sursa (job #2493305) | Cod sursa (job #2836735) | Cod sursa (job #112019) | Cod sursa (job #119876)
Cod sursa(job #119876)
const fi='ordine.in';
fo='ordine.out';
var
f:text;
A:array[1..64060] of char;
Ap:Array['a'..'z'] of longint;
i,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);
u:=0; uc:='a';
while u<k do
begin
c:=uc;
while Ap[c]=0 do
c:=succ(c);
uc:=c;
u:=u+1;
ch:=uc;
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
c:=uc;
while Ap[c]=0 do
c:=succ(c);
if c=A[u-1] then begin
ch:=Succ(uc);
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.