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. for i:=0 to len do funz_errore[i]:=-1; (*inizializzo la funzione errore a -1*)
  17. K:=0;
  18. for j:=1 to len do
  19. begin
  20. i:=funz_errore[j - k - 1];
  21. while (i <> -1 ) and (x[j mod len] <> x[(k + i + 1 ) mod len]) do begin
  22. if x[j mod len] < x[(k + i + 1) mod len] then k:= j - i - 1;
  23. i:= funz_errore[i]; end;
  24. if (i = -1) and (s[j mod len ] <> s[(k + i+1 ) mod len ]) then
  25. begin
  26. if s[j mod len] < s[(k + i+1 ) mod len] then k:= j;
  27. funz_errore[j - k]:= -1;
  28. end
  29. else
  30. funz_errore[j - k]:= i + 1;
  31.  
  32. end;
  33. LexicalMinRotation:=k+1;
  34.  
  35. end;
  36.  
  37. begin
  38. (*assign(input, 'input.txt'); reset(input);
  39.   assign(output, 'output.txt'); rewrite(output);*)
  40. readln (N,M);
  41. for w:=0 to N-1 do readln(S[w]);
  42.  
  43.  
  44. coppie:=0;
  45. for w:=0 to N-1 do
  46. begin
  47. index:=LexicalMinRotation(S[w]);
  48. writeln(index);
  49. S_ruotate[w]:=copy(temp,index,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.
  60.  
Success #stdin #stdout 0.02s 5288KB
stdin
4 4
abcd
xbcd
cdab
dabc
stdout
1
abcd
1
xbcd
1
cdab
1
dabc
0