Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const fi='P132SUMJ.inp';
- fo='P132SUMJ.out';
- var s, ss, ans, tmp: string;
- n, i, l: longint;
- f: array [1..6] of boolean;
- find: boolean;
- procedure thu;
- var i: longint;
- begin
- if l=n then
- begin
- if ans>ss then
- begin
- writeln(ans);
- find:=true;
- end;
- end
- else
- begin
- for i:=1 to n do
- if not f[i] then
- begin
- inc(l); ans[l]:=ss[i]; f[i]:=true;
- thu;
- if find then exit;
- f[i]:=false; dec(l);
- end;
- end;
- end;
- procedure Sort(l, r: longint);
- var i, j: longint;
- m, tmp: char;
- begin
- if l>=r then exit;
- i:=l; j:=r; m:=ss[(l+r) div 2];
- while i<=j do
- begin
- while ss[i]<m do inc(i);
- while ss[j]>m do dec(j);
- if i<=j then
- begin
- tmp:=ss[i]; ss[i]:=ss[j]; ss[j]:=tmp;
- inc(i); dec(j);
- end;
- end;
- Sort(l, j); Sort(i, r);
- end;
- begin
- assign(input, fi); reset(input);
- assign(output, fo); rewrite(output);
- readln(s);
- n:=length(s); ss:=s; ans:=s;
- tmp:=''; for i:=n downto 1 do tmp:=tmp+s[i];
- sort(1, n);
- if tmp=s then writeln(0) else
- begin
- fillchar(f, sizeof(f), false); l:=0;
- thu;
- if not find then writeln(0);
- end;
- close(input); close(output);
- end.
Advertisement
Add Comment
Please, Sign In to add comment