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. function LexicalMinRotation(var x: AnsiString):Int64;
  11. var
  12. len,K,i,j:Int64;
  13.  
  14. begin
  15. len:=length(x);
  16. for i:=0 to len do funz_errore[i]:=-1;
  17. K:=1;
  18. for j:=2 to len do
  19. begin
  20. i:=funz_errore[j - k-1 ];
  21. while (i <> -1 ) and (x[j] <> x[(k + i )]) do
  22. begin
  23. if x[j] < x[(k + i )] then k:= j - i-1 ;
  24. i:= funz_errore[i+1];
  25. end;
  26. if (i = -1) and (x[j] <> x[(k + i )]) then
  27. begin
  28. if x[j] < x[(k + i )] then k:= j;
  29. funz_errore[j - k]:= -1;
  30. end
  31. else funz_errore[j - k]:= i + 1;
  32.  
  33. end;
  34. LexicalMinRotation:=k;
  35.  
  36. end;
  37.  
  38. begin
  39. (*assign(input, 'input.txt'); reset(input);
  40.   assign(output, 'output.txt'); rewrite(output);*)
  41. readln (N,M);
  42. for w:=0 to N-1 do begin readln(S[w]); S[w]:=Trim(S[w]); end;
  43. coppie:=0;
  44. for w:=0 to N-1 do
  45. begin
  46. index:=LexicalMinRotation(S[w]);
  47. temp:=S[w]+S[w];
  48. S_ruotate[w]:=copy(temp,index,M);
  49. writeln (S_ruotate[w]);
  50. end;
  51. for w:=0 to N-2 do
  52. for v:=w+1 to N-1 do
  53. begin
  54. C:= CompareStr(S_ruotate[w], S_ruotate[v]);
  55. if C=0 then coppie:=coppie+1;
  56. end;
  57. writeln (coppie);
  58. end.
Success #stdin #stdout 0.01s 5284KB
stdin
2 4
abcd
cdab
dabc

stdout
abcd
cdab
0