Cod sursa(job #109694)

Utilizator generalsenobiScinteie Sebastian generalsenobi Data 25 noiembrie 2007 12:26:51
Problema Ordine Scor 0
Compilator fpc Status done
Runda preONI 2008, Runda 1, Clasele 5-8 Marime 0.96 kb
Program Ordine;

var
  s: string;
  c: char;
  i, j, l: LongInt;
  cg: LongInt;
  fin,fout:text;

  Function Conflict(poz:LongInt):Boolean;
  Begin
    Conflict := False;
    if poz>1 then if s[poz]=s[poz-1] then Conflict:=True;
    if poz<length(s) then if s[poz]=s[poz+1] then Conflict:=True;
  End;

begin
assign(fin,'ordine.in'); reset(fin);
assign(fout,'ordine.out'); rewrite(fout);
  Readln(fin,s);
  l:=length(s);
  cg:=0;
  repeat
    for i:= l downto 2 do
    begin
    cg:=0;
    For j:=1 to i-1 do
      Begin
        if (s[i]<s[j]) then
          begin
            c:=s[j];
            s[j]:=s[i];
            s[i]:=c;
            inc(cg);

            if conflict(i) or conflict(j) then
            begin
              c:=s[j];
              s[j]:=s[i];
              s[i]:=c;
              dec(cg);
            end;
          end;
      end;
    end;
  until cg=0;
  writeln(fout,s);
  close(fin);close(fout);

end.