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. index:=LexicalMinRotation(S[0]);
  47. S_ruotate[0]:=copy(S[0],index,M);
  48. for w:=1 to N-1 do
  49. begin
  50. index:=LexicalMinRotation(S[w]);
  51. S_ruotate[w]:=copy(S[w],index,M);
  52. if S_ruotate[w]=S_ruotate[0] then coppie:=coppie+1;
  53. end;
  54.  
  55. writeln(coppie);
  56. end.
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
Success #stdin #stdout 0.02s 5288KB
stdin
4 4
abcd
xbcd
cdab
dabc
stdout
2