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