fork download
  1. program casino;
  2. Uses sysutils;
  3. {$H+}
  4. const lung=1000000;
  5. var N,M,C,w,v,coppie,index:Int64;
  6. temp: AnsiString;
  7. stringa:array[0..lung] of char;
  8. S,S_ruotate:array[0..lung] of AnsiString;
  9. funz_errore : array[0..2000000] of Int64;
  10.  
  11. function LexicalMinRotation(var x: AnsiString):Int64;
  12. var
  13. len,K,i,j:Int64;
  14.  
  15. begin
  16. len:=length(x);
  17. for i:=0 to len do funz_errore[i]:=-1;
  18. K:=1;
  19. for j:=2 to len do
  20. begin
  21. i:=funz_errore[j - k-1 ];
  22. while (i <> -1 ) and (x[j] <> x[(k + i )]) do
  23. begin
  24. if x[j] < x[(k + i )] then k:= j - i - 1;
  25. i:= funz_errore[i];
  26. end;
  27. if (i = -1) and (x[j] <> x[(k + i+1 )]) then
  28. begin
  29. if x[j] < x[(k + i+1 )] then k:= j;
  30. funz_errore[j - k+1]:= -1;
  31. end
  32. else funz_errore[j - k]:= i + 1;
  33.  
  34. end;
  35. LexicalMinRotation:=k;
  36. writeln(K);
  37. end;
  38.  
  39. begin
  40. (*assign(input, 'input.txt'); reset(input);
  41.   assign(output, 'output.txt'); rewrite(output);*)
  42. readln (N,M);
  43. for w:=0 to N-1 do begin readln(S[w]); S[w]:=Trim(S[w]); end;
  44. coppie:=0;
  45. for w:=0 to N-1 do
  46. begin
  47.  
  48. index:=LexicalMinRotation(S[w]);
  49. temp:=S[w]+S[w];
  50. S_ruotate[w]:=copy(temp,index,M);
  51. writeln(S_ruotate[w]);
  52. end;
  53. for w:=0 to N-2 do
  54. for v:=w+1 to N-1 do
  55. begin
  56. C:= CompareStr(S_ruotate[w], S_ruotate[v]);
  57. if C=0 then coppie:=coppie+1;
  58. end;
  59. writeln (coppie);
  60. end.
Success #stdin #stdout 0.02s 5288KB
stdin
3 6
adaada
aaadda
aadaad
stdout
1
adaada
1
aaadda
1
aadaad
0