fork download
  1. import java.io.*;
  2. import java.math.BigInteger;
  3. import java.util.*;
  4.  
  5. class CyclicHashes implements Runnable {
  6.  
  7. private BufferedReader in;
  8. private PrintWriter out;
  9.  
  10. void init() throws FileNotFoundException {
  11. out = new PrintWriter(System.out);
  12. }
  13.  
  14. public static void main(String[] args) {
  15. new Thread(null, new CyclicHashes(), "", 1l * 200 * 1024 * 1024).start();
  16. }
  17.  
  18. public void run() {
  19. try {
  20. init();
  21. solve();
  22. out.close();
  23. } catch (Exception e) {
  24. e.printStackTrace();
  25. System.exit(-1);
  26. }
  27. }
  28.  
  29. int n;
  30. int prime1, prime2;
  31. int mod1, mod2;
  32. int[] pow1, pow2;
  33. int[] h1, h2;
  34.  
  35. void init(char[] s) {
  36. Random rnd = new Random();
  37. prime1 = BigInteger.valueOf(3000 + rnd.nextInt(500))
  38. .nextProbablePrime().intValue();
  39. prime2 = BigInteger.valueOf(4000 + rnd.nextInt(500))
  40. .nextProbablePrime().intValue();
  41. mod1 = BigInteger.valueOf((int) 1e9 + rnd.nextInt(500))
  42. .nextProbablePrime().intValue();
  43. mod2 = BigInteger.valueOf((int) 1e9 + (int) 1e6 + rnd.nextInt(500))
  44. .nextProbablePrime().intValue();
  45. n = s.length;
  46. pow1 = new int[n];
  47. pow2 = new int[n];
  48. pow1[0] = pow2[0] = 1;
  49. for (int i = 1; i < n; i++) {
  50. pow1[i] = (int) ((long) pow1[i - 1] * prime1 % mod1);
  51. pow2[i] = (int) ((long) pow2[i - 1] * prime2 % mod2);
  52. }
  53. h1 = new int[n];
  54. h2 = new int[n];
  55. h1[0] = h2[0] = s[0];
  56. for (int i = 1; i < n; i++) {
  57. h1[i] = (int) ((h1[i - 1] + (long) pow1[i] * s[i]) % mod1);
  58. h2[i] = (int) ((h2[i - 1] + (long) pow2[i] * s[i]) % mod2);
  59. }
  60. }
  61.  
  62. long getHash(int L, int R) {
  63. int hash1 = getHash(L, R, h1, pow1, mod1);
  64. int hash2 = getHash(L, R, h2, pow2, mod2);
  65. return ((long) hash1 << 32) | hash2;
  66. }
  67.  
  68. int getHash(int L, int R, int[] h, int[] pow, int mod) {
  69. int hash = h[R];
  70. if (L > 0)
  71. hash -= h[L - 1];
  72. if (hash < 0)
  73. hash += mod;
  74. return (int) ((long) hash * pow[n - R - 1] % mod);
  75. }
  76.  
  77. boolean equals(int L1, int R1, int L2, int R2) {
  78. long hash1 = getHash(L1, R1);
  79. long hash2 = getHash(L2, R2);
  80. return hash1 == hash2;
  81. }
  82.  
  83. void solve() throws IOException {
  84. int n = Integer.parseInt(in.readLine());
  85. String s = in.readLine();
  86. String t = in.readLine();
  87. StringBuilder sb = new StringBuilder();
  88. sb.append(s);
  89. sb.append(t);
  90. sb.append(t);
  91. char[] res = sb.toString().toCharArray();
  92.  
  93. init(res);
  94.  
  95. for (int shift = 0; shift < n; shift++) {
  96. if (equals(0, n - 1, n + shift, 2 * n - 1 + shift)) {
  97. out.println(shift);
  98. return;
  99. }
  100. }
  101. out.println(-1);
  102.  
  103. }
  104.  
  105. }
Success #stdin #stdout 0.06s 320704KB
stdin
11
abracadabra
racadabraab
stdout
9