Problem p02962 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5 + 10;
char laz[maxn];
int pi[10 * maxn];
bool prefix_function(const string &s, const string &t) {
  /// search t on s
  int m = (int)t.length();
  string ss = t + '#' + s;
  int n = (int)ss.length();
  pi[0] = 0;
  for (int i = 1; i < n; i++) {
    int j = pi[i - 1];
    while (j > 0 && ss[i] != ss[j])
      j = pi[j - 1];
    if (ss[i] == ss[j])
      j++;
    pi[i] = j;
    if (pi[i] == m && i > m) {
      return true;
    }
  }
  return false;
}
string s, t, a, ss, tt;
/**
There exists a non-negative integer j
such that the concatenation of i copies of t
is a substring of the concatenation of j copies of s.
*/

int main() {
  scanf("%s", laz);
  ss = string(laz);
  scanf("%s", laz);
  t = string(laz);
  s = ss;
  while ((int)s.length() <= (int)t.length() + (int)ss.length()) {
    s += ss;
  }
  s += ss;
  int l = 1, r = ((int)s.length()) / ((int)t.length()), mid, c = -1;
  while (l <= r) {
    mid = (l + r) >> 1;
    a = t;
    for (int i = 1; i < mid; i++) {
      a += t;
    }
    if (prefix_function(s, a)) {
      c = mid;
      l = mid + 1;
    } else {
      r = mid - 1;
    }
  }
  if (c == -1) {
    return !printf("0");
  }
  s += s;
  l = 1, r = ((int)s.length()) / ((int)t.length());
  int cc = -1;
  while (l <= r) {
    mid = (l + r) >> 1;
    a = t;
    for (int i = 1; i < mid; i++) {
      a += t;
    }
    if (prefix_function(s, a)) {
      cc = mid;
      l = mid + 1;
    } else {
      r = mid - 1;
    }
  }
  if (cc == c) {
    return !printf("%d\n", cc);
  } else {
    return !printf("-1");
  }
}
/*
    Good Luck
        -Lci70121
*/


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5 + 10;
char laz[maxn];
int pi[16 * maxn];
bool prefix_function(const string &s, const string &t) {
  /// search t on s
  int m = (int)t.length();
  string ss = t + '#' + s;
  int n = (int)ss.length();
  pi[0] = 0;
  for (int i = 1; i < n; i++) {
    int j = pi[i - 1];
    while (j > 0 && ss[i] != ss[j])
      j = pi[j - 1];
    if (ss[i] == ss[j])
      j++;
    pi[i] = j;
    if (pi[i] == m && i > m) {
      return true;
    }
  }
  return false;
}
string s, t, a, ss, tt;
/**
There exists a non-negative integer j
such that the concatenation of i copies of t
is a substring of the concatenation of j copies of s.
*/

int main() {
  scanf("%s", laz);
  ss = string(laz);
  scanf("%s", laz);
  t = string(laz);
  s = ss;
  while ((int)s.length() <= (int)t.length() + (int)ss.length()) {
    s += ss;
  }
  s += ss;
  int l = 1, r = ((int)s.length()) / ((int)t.length()), mid, c = -1;
  while (l <= r) {
    mid = (l + r) >> 1;
    a = t;
    for (int i = 1; i < mid; i++) {
      a += t;
    }
    if (prefix_function(s, a)) {
      c = mid;
      l = mid + 1;
    } else {
      r = mid - 1;
    }
  }
  if (c == -1) {
    return !printf("0");
  }
  s += s;
  l = 1, r = ((int)s.length()) / ((int)t.length());
  int cc = -1;
  while (l <= r) {
    mid = (l + r) >> 1;
    a = t;
    for (int i = 1; i < mid; i++) {
      a += t;
    }
    if (prefix_function(s, a)) {
      cc = mid;
      l = mid + 1;
    } else {
      r = mid - 1;
    }
  }
  if (cc == c) {
    return !printf("%d\n", cc);
  } else {
    return !printf("-1");
  }
}
/*
    Good Luck
        -Lci70121
*/