giabinh

P132SUMJ_HoanViChuSo

Jun 14th, 2015
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. const fi='P132SUMJ.inp';
  2. fo='P132SUMJ.out';
  3.  
  4. var s, ss, ans, tmp: string;
  5. n, i, l: longint;
  6. f: array [1..6] of boolean;
  7. find: boolean;
  8.  
  9. procedure thu;
  10. var i: longint;
  11. begin
  12. if l=n then
  13. begin
  14. if ans>ss then
  15. begin
  16. writeln(ans);
  17. find:=true;
  18. end;
  19. end
  20. else
  21. begin
  22. for i:=1 to n do
  23. if not f[i] then
  24. begin
  25. inc(l); ans[l]:=ss[i]; f[i]:=true;
  26. thu;
  27. if find then exit;
  28. f[i]:=false; dec(l);
  29. end;
  30. end;
  31. end;
  32.  
  33. procedure Sort(l, r: longint);
  34. var i, j: longint;
  35. m, tmp: char;
  36. begin
  37. if l>=r then exit;
  38. i:=l; j:=r; m:=ss[(l+r) div 2];
  39. while i<=j do
  40. begin
  41. while ss[i]<m do inc(i);
  42. while ss[j]>m do dec(j);
  43. if i<=j then
  44. begin
  45. tmp:=ss[i]; ss[i]:=ss[j]; ss[j]:=tmp;
  46. inc(i); dec(j);
  47. end;
  48. end;
  49. Sort(l, j); Sort(i, r);
  50. end;
  51.  
  52. begin
  53. assign(input, fi); reset(input);
  54. assign(output, fo); rewrite(output);
  55.  
  56. readln(s);
  57. n:=length(s); ss:=s; ans:=s;
  58. tmp:=''; for i:=n downto 1 do tmp:=tmp+s[i];
  59. sort(1, n);
  60.  
  61. if tmp=s then writeln(0) else
  62. begin
  63. fillchar(f, sizeof(f), false); l:=0;
  64. thu;
  65. if not find then writeln(0);
  66. end;
  67.  
  68. close(input); close(output);
  69. end.
Advertisement
Add Comment
Please, Sign In to add comment