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,PP, z :qword;
  6. numero, count : qword;
  7. cod : longint;
  8. cifre, X, stringadainvertire, stringainvertita, stringaordinata, Sconinv : Ansistring;
  9. ccifre : array[1..MAX] of integer;
  10. potenzadieci : array [0..MAX] of qword;
  11. arrnum, P :array [1..MAX] of string[1];
  12.  
  13. procedure scambia (var x,y: string);
  14. var t:string;
  15. begin
  16. t:=x;
  17. x:=y;
  18. y:=t;
  19. end;
  20. Procedure ordinamento (estremoi,estremos: qword; var v : elenco; ordinato:boolean);
  21. var inf, sup, medio:qword;
  22. pivot :string[1];
  23. begin
  24. inf:=estremoi;
  25. sup:=estremos;
  26. medio:= (estremoi+estremos) div 2;
  27. pivot:=v[medio];
  28. repeat
  29. if (ordinato) then
  30. begin
  31. while (v[inf]<pivot) do inf:=inf+1;
  32. while (v[sup]>pivot) do sup:=sup-1;
  33. end;
  34. if inf<=sup then
  35. begin
  36. scambia(v[inf],v[sup]);
  37. inf:=inf+1;
  38. sup:=sup-1;
  39. end;
  40. until inf>sup;
  41. if (estremoi<sup) then ordinamento(estremoi,sup,v,ordinato);
  42. if (inf<estremos) then ordinamento(inf,estremos,v,ordinato);
  43. end;
  44. Procedure inversione (x:Ansistring; inizio:qword; var Sconinv:ansistring);
  45. var Sdainv,Sprefix : ansistring;
  46. begin
  47. count:=m-inizio+1; Sprefix:='';
  48. Sdainv:=copy(x,inizio, count);
  49. Sprefix:=copy(x, 1, inizio-2);
  50. stringainvertita:=ReverseString(Sdainv);
  51. Sconinv:=Sprefix+stringainvertita;
  52. end;
  53.  
  54. Procedure nextPermutation (P : elenco) ;
  55. var pivot:qword;
  56. (* Find the pivot index*)
  57. begin
  58. pivot := 0;
  59. for i := m - 1 downto 1 do
  60. if (P[i] < P[i + 1]) then begin pivot:=i; break; end;
  61. writeln(pivot);
  62. if pivot = 0 then exit;
  63. (* find the element from the right that is greater than pivot*)
  64. for i := m downto pivot+1 do
  65. begin
  66. if (P[i] > P[pivot]) then scambia(P[i], P[pivot]);
  67. break;
  68. end;
  69.  
  70. (* Reverse the elements from pivot + 1 to the end to get the next permutation*)
  71. if pivot+1=m then for i:=1 to m do Sconinv:=Sconinv+P[i]
  72. else inversione (stringadainvertire, pivot+1, Sconinv);
  73.  
  74. stringadainvertire:=Sconinv;
  75. for i:=1 to m do arrnum[i]:=P[i];
  76.  
  77. end;
  78.  
  79. begin
  80. readln(m,k);
  81. readln(cifre);
  82. (* X:=ReverseString(cifre); writeln (X);*)
  83. for i:=1 to m do arrnum[i]:=copy(cifre,i,1);
  84. ordinamento (1,m,arrnum, true);
  85. stringaordinata:=''; Sconinv:='';
  86. for i:=1 to m do stringaordinata:=stringaordinata+arrnum[i];
  87. (*val(cifre,numero,cod);
  88.   writeln (numero);
  89.   inversione (cifre, 3, stringainvertita);
  90.   writeln(stringainvertita);*)
  91. stringadainvertire:=stringaordinata;
  92. for i:=1 to m do write(arrnum[i]); writeln('oo');
  93.  
  94. for z:=1 to 3 do begin nextPermutation(arrnum); writeln(Sconinv); end;
  95. end.
  96.  
Success #stdin #stdout 0.01s 12320KB
stdin
6 3
480912
stdout
012489oo
5
012498
4
01289
4
0129