fork download
  1. program casino;
  2. Uses sysutils;
  3. {$H+}
  4. const lung=1000000;
  5. type elenco=array[0..lung-1] of Qword;
  6. var N,M,w,v,C,t,coppie,index:Qword;
  7. S,S_ruotate:array[0..lung] of AnsiString;
  8. funz_errore: array[0..1000000] of Int64;
  9. H :elenco;
  10. accoppiata: array[0..1000000] of boolean;
  11.  
  12. function LexicalMinRotation(var x: AnsiString):Qword;
  13. var
  14. len,K,j:Qword;
  15. i:Int64;
  16.  
  17. begin
  18. x:=x+x;
  19. len:=length(x);
  20. for i:=0 to len do funz_errore[i]:=-1;
  21. K:=1;
  22. for j:=2 to len do
  23. begin
  24. i:= funz_errore[j-k-1];
  25. while (i <> -1 ) and (x[j] <> x[(k + i+1 )]) do
  26. begin
  27. if x[j] < x[(k + i+1 )] then k:= j - i - 1;
  28. i:=funz_errore[i];
  29. end;
  30. if (i = -1) and (x[j] <> x[(k + i+1 )]) then
  31. begin
  32. if x[j] < x[(k + i+1 )] then k:= j;
  33. funz_errore[j - k]:= -1;
  34. end
  35. else funz_errore[j - k]:= i + 1;
  36.  
  37. end;
  38. LexicalMinRotation:=k;
  39.  
  40. end;
  41.  
  42. function Rabin (var x: Ansistring) :Qword;
  43. var len, i, R,base,d,q:Qword;
  44. begin
  45. d:=1; R:=0; len:=length(x); base:=26; q:=8446744073709551615;
  46. for i := 1 to len-1 do d := (d * base) mod q ;
  47. for i := 1 to len do R:= (base*R + (ord(x[i])) ) mod q;
  48. Rabin:= R;
  49. end;
  50.  
  51.  
  52.  
  53. begin
  54. (*assign(input, 'input.txt'); reset(input);
  55.   assign(output, 'output.txt'); rewrite(output);*)
  56. readln (N,M);
  57. for w:=0 to N-1 do begin readln(S[w]); S[w]:=Trim(S[w]); end;
  58. for w:=0 to N-1 do begin H[w]:=0; accoppiata[w]:=false;end;
  59. coppie:=0;
  60. for w:=0 to N-1 do
  61. begin
  62. index:=LexicalMinRotation(S[w]);
  63. S_ruotate[w]:=copy(S[w],index,M);
  64. H[w]:=Rabin(S_ruotate[w]);
  65. end;
  66. for w:=0 to N-2 do
  67. if accoppiata[w]=false then begin
  68. for v:=w+1 to N-1 do
  69. begin
  70. if (H[w]=H[v]) then
  71. begin
  72. C:= CompareStr(S_ruotate[w], S_ruotate[v]);
  73. if C=0 then begin coppie:=coppie+1; accoppiata[w]:=true; accoppiata[v]:=true; end;
  74. end;
  75.  
  76. writeln(w,' ',v);
  77. end;
  78.  
  79. end;
  80.  
  81. writeln (coppie);
  82. for w:= 0 to N-1 do writeln(accoppiata[w]);
  83. end.
Success #stdin #stdout 0.02s 5284KB
stdin
4 4
abcd
xbcd
cdab
dabc
stdout
0 1
0 2
0 3
1 2
1 3
2
TRUE
FALSE
TRUE
TRUE