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