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. S,S_ruotate:array[0..lung] of AnsiString;
  7. funz_errore : array[0..2000000] of Int64;
  8.  
  9. function LexicalMinRotation(var x: AnsiString):Int64;
  10. var
  11. len,K,i,j:Int64;
  12.  
  13. begin
  14. x:=x+x;
  15. len:=length(x);
  16. for i:=0 to len do funz_errore[i]:=-1;
  17. K:=1;
  18. j:=2;
  19. while j<=len do begin
  20. i:= funz_errore[j-k-1];
  21. while (i <> -1 ) and (x[j] <> x[(k + i+1 )]) and (j<=len ) do
  22. begin
  23.  
  24. if x[j] < x[(k + i+1 )] 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;
  31. end
  32. else funz_errore[j - k]:= i + 1;
  33.  
  34.  
  35. j:=j+1; end;
  36. LexicalMinRotation:=k;
  37.  
  38. end;
  39.  
  40. begin
  41. (*assign(input, 'input.txt'); reset(input);
  42.   assign(output, 'output.txt'); rewrite(output);*)
  43. readln (N,M);
  44. for w:=0 to N-1 do begin readln(S[w]); S[w]:=Trim(S[w]); end;
  45. coppie:=0;
  46. for w:=0 to N-1 do
  47. begin
  48. index:=LexicalMinRotation(S[w]);
  49. S_ruotate[w]:=copy(S[w],index,M);
  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 5288KB
stdin
4 4
abcd
xbcd
cdab
dabc
stdout
3