fork download
  1. program wardrobe3;
  2. Uses Strutils;
  3. const MAX = 5000000;
  4. type elenco = array[1..MAX] of string[1];
  5. var m,k,i :qword;
  6. numero : qword;
  7. cifre, X : Ansistring;
  8. ccifre : array[1..MAX] of integer;
  9. potenzadieci : array [0..MAX] of qword;
  10. arrnum:array [1..MAX] of string[1];
  11.  
  12. procedure scambia (var x,y: string);
  13. var t:string;
  14. begin
  15. t:=x;
  16. x:=y;
  17. y:=t;
  18. end;
  19. Procedure ordinamento (estremoi,estremos: qword; var v : elenco; ordinato:boolean);
  20. var inf, sup, medio:qword;
  21. pivot :string[1];
  22. begin
  23. inf:=estremoi;
  24. sup:=estremos;
  25. medio:= (estremoi+estremos) div 2;
  26. pivot:=v[medio];
  27. repeat
  28. if (ordinato) then
  29. begin
  30. while (v[inf]<pivot) do inf:=inf+1;
  31. while (v[sup]>pivot) do sup:=sup-1;
  32. end;
  33. if inf<=sup then
  34. begin
  35. scambia(v[inf],v[sup]);
  36. inf:=inf+1;
  37. sup:=sup-1;
  38. end;
  39. until inf>sup;
  40. if (estremoi<sup) then ordinamento(estremoi,sup,v,ordinato);
  41. if (inf<estremos) then ordinamento(inf,estremos,v,ordinato);
  42. end;
  43.  
  44. procedure next_permutation (p : elenco);
  45. var pivot :int64;
  46. begin
  47. (*Find the pivot index*)
  48. pivot := -1;
  49. for i := m - 2 downto 0 do
  50. begin
  51. if p[i] < p[i + 1] then
  52. begin
  53. pivot := i;
  54. break;
  55. end;
  56. end;
  57. (*find the element from the right that is greater than pivot*)
  58. for i := m - 1 downto pivot+1 do
  59. begin
  60. if p[i] > p[pivot] then
  61. begin
  62. scambia(p[i], p[pivot]);
  63. break;
  64. end;
  65. end;
  66. end;
  67.  
  68. begin
  69. readln(m,k);
  70. readln(cifre);
  71. X:=ReverseString(cifre);
  72. for i:=1 to m do arrnum[i]:=copy(cifre,i,1);
  73. for i:=1 to m do write(arrnum[i]); writeln;
  74. potenzadieci[0]:=1; numero:=0;
  75. for i:=1 to m do potenzadieci[i]:=(potenzadieci[i-1]*10);
  76. for i:=1 to m do begin numero:=numero + potenzadieci[m-i]*(ord(cifre[i]) - 48); end;
  77. ordinamento (1,m,arrnum, true);
  78. for i:=1 to m do write(arrnum[i]); writeln;
  79. next_permutation(arrnum);
  80. for i:=1 to m do write(arrnum[i]); writeln;
  81. end.
Success #stdin #stdout 0.01s 14364KB
stdin
6 3
123042
stdout
123042
012234
012234