Problem p02962 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define For(i, a, b) for (int(i) = (a); (i) < (b); ++(i))
#define rFor(i, a, b) for (int(i) = (a)-1; (i) >= (b); --(i))
#define rep(i, n) For((i), 0, (n))
#define rrep(i, n) rFor((i), (n), 0)
#define fi first
#define se second
using namespace std;
typedef long long lint;
typedef pair<int, int> pii;

const lint b1 = 1009;
const lint b2 = 1007;
const lint b3 = 997;
const lint h1 = 1e9 + 7;
const lint h2 = 1e9 + 9;
const lint h3 = 998244353;

lint powmod(lint x, lint n, lint mod) {
  lint ret = 1;
  while (n > 0) {
    if (n & 1)
      ret *= x, ret %= mod, n -= 1;
    else
      x *= x, x %= mod, n >>= 1;
  }
  return ret;
}

int d[500010], n, m;
vector<int> G[500010];

void scc_dfs(int v, vector<bool> &used, vector<int> &vs) {
  used[v] = true;
  for (int nv : G[v])
    if (!used[nv])
      scc_dfs(nv, used, vs);
  vs.push_back(v);
}

void scc_rdfs(int v, int k, vector<int> &cmp, vector<bool> &used,
              vector<vector<int>> &rG) {
  used[v] = true;
  cmp[v] = k;
  for (int nv : rG[v])
    if (!used[nv])
      scc_rdfs(nv, k, cmp, used, rG);
}

pair<int, vector<int>> scc() {
  vector<vector<int>> rG(n);
  rep(i, n) for (int nv : G[i]) rG[nv].push_back(i);
  vector<bool> used(n, false);
  vector<int> vs;
  vector<int> cmp(n);
  rep(i, n) if (!used[i]) scc_dfs(i, used, vs);
  fill(used.begin(), used.end(), false);
  int k = 0;
  rrep(i, n) if (!used[vs[i]]) scc_rdfs(vs[i], k++, cmp, used, rG);
  return make_pair(k, cmp);
}

int longest_path(int v) {
  if (d[v] >= 0)
    return d[v];
  int ret = 0;
  for (int nv : G[v])
    ret = max(ret, longest_path(nv) + 1);
  return d[v] = ret;
}

int main() {
  string s, t;
  cin >> s >> t;
  n = s.size();
  m = t.size();
  lint th1 = 0;
  lint th2 = 0;
  lint th3 = 0;
  rep(i, m) {
    th1 = (th1 * b1 + (t[i] - 'a')) % h1;
    th2 = (th2 * b2 + (t[i] - 'a')) % h2;
    th3 = (th3 * b3 + (t[i] - 'a')) % h3;
  }
  lint sh1 = 0;
  lint sh2 = 0;
  lint sh3 = 0;
  rep(i, m) {
    sh1 = (sh1 * b1 + (s[i % n] - 'a')) % h1;
    sh2 = (sh2 * b2 + (s[i % n] - 'a')) % h2;
    sh3 = (sh3 * b3 + (s[i % n] - 'a')) % h3;
  }
  int num[n];
  rep(i, n) num[i] = 0;
  if (sh1 == th1 && sh2 == th2 && sh3 == th3)
    ++num[0];
  lint p1 = powmod(b1, m, h1);
  lint p2 = powmod(b2, m, h2);
  lint p3 = powmod(b3, m, h3);
  rep(i, n - 1) {
    sh1 =
        (sh1 * b1 % h1 - p1 * (s[i] - 'a') % h1 + (s[(i + m) % n] - 'a') + h1) %
        h1;
    sh2 =
        (sh2 * b2 % h2 - p2 * (s[i] - 'a') % h2 + (s[(i + m) % n] - 'a') + h2) %
        h2;
    sh3 =
        (sh3 * b3 % h3 - p3 * (s[i] - 'a') % h3 + (s[(i + m) % n] - 'a') + h3) %
        h3;
    if (sh1 == th1 && sh2 == th2 && sh3 == th3)
      ++num[i + 1];
  }
  rep(i, n) {
    if (num[i])
      G[i].push_back((i + m) % n);
    d[i] = -1;
  }
  auto scc_result = scc();
  if (scc_result.fi < n) {
    printf("-1");
    return 0;
  }
  int ans = 0;
  rep(i, n) ans = max(ans, longest_path(i));
  printf("%d\n", ans);
}

Fixed submission:
#include <bits/stdc++.h>
#define For(i, a, b) for (int(i) = (a); (i) < (b); ++(i))
#define rFor(i, a, b) for (int(i) = (a)-1; (i) >= (b); --(i))
#define rep(i, n) For((i), 0, (n))
#define rrep(i, n) rFor((i), (n), 0)
#define fi first
#define se second
using namespace std;
typedef long long lint;
typedef pair<int, int> pii;

const lint b1 = 1009;
const lint b2 = 1007;
const lint b3 = 997;
const lint h1 = 1e9 + 7;
const lint h2 = 1e9 + 9;
const lint h3 = 998244353;

lint powmod(lint x, lint n, lint mod) {
  lint ret = 1;
  while (n > 0) {
    if (n & 1)
      ret *= x, ret %= mod, n -= 1;
    else
      x *= x, x %= mod, n >>= 1;
  }
  return ret;
}

int d[500010], n, m;
vector<int> G[500010];

void scc_dfs(int v, vector<bool> &used, vector<int> &vs) {
  used[v] = true;
  for (int nv : G[v])
    if (!used[nv])
      scc_dfs(nv, used, vs);
  vs.push_back(v);
}

void scc_rdfs(int v, int k, vector<int> &cmp, vector<bool> &used,
              vector<vector<int>> &rG) {
  used[v] = true;
  cmp[v] = k;
  for (int nv : rG[v])
    if (!used[nv])
      scc_rdfs(nv, k, cmp, used, rG);
}

pair<int, vector<int>> scc() {
  vector<vector<int>> rG(n);
  rep(i, n) for (int nv : G[i]) rG[nv].push_back(i);
  vector<bool> used(n, false);
  vector<int> vs;
  vector<int> cmp(n);
  rep(i, n) if (!used[i]) scc_dfs(i, used, vs);
  fill(used.begin(), used.end(), false);
  int k = 0;
  rrep(i, n) if (!used[vs[i]]) scc_rdfs(vs[i], k++, cmp, used, rG);
  return make_pair(k, cmp);
}

int longest_path(int v) {
  if (d[v] >= 0)
    return d[v];
  int ret = 0;
  for (int nv : G[v])
    ret = max(ret, longest_path(nv) + 1);
  return d[v] = ret;
}

int main() {
  string s, t;
  cin >> s >> t;
  n = s.size();
  m = t.size();
  lint th1 = 0;
  lint th2 = 0;
  lint th3 = 0;
  rep(i, m) {
    th1 = (th1 * b1 + (t[i] - 'a')) % h1;
    th2 = (th2 * b2 + (t[i] - 'a')) % h2;
    th3 = (th3 * b3 + (t[i] - 'a')) % h3;
  }
  lint sh1 = 0;
  lint sh2 = 0;
  lint sh3 = 0;
  rep(i, m) {
    sh1 = (sh1 * b1 + (s[i % n] - 'a')) % h1;
    sh2 = (sh2 * b2 + (s[i % n] - 'a')) % h2;
    sh3 = (sh3 * b3 + (s[i % n] - 'a')) % h3;
  }
  int num[n];
  rep(i, n) num[i] = 0;
  if (sh1 == th1 && sh2 == th2 && sh3 == th3)
    ++num[0];
  lint p1 = powmod(b1, m, h1);
  lint p2 = powmod(b2, m, h2);
  lint p3 = powmod(b3, m, h3);
  rep(i, n - 1) {
    sh1 =
        (sh1 * b1 % h1 - p1 * (s[i] - 'a') % h1 + (s[(i + m) % n] - 'a') + h1) %
        h1;
    sh2 =
        (sh2 * b2 % h2 - p2 * (s[i] - 'a') % h2 + (s[(i + m) % n] - 'a') + h2) %
        h2;
    sh3 =
        (sh3 * b3 % h3 - p3 * (s[i] - 'a') % h3 + (s[(i + m) % n] - 'a') + h3) %
        h3;
    if (sh1 == th1 && sh2 == th2 && sh3 == th3)
      ++num[i + 1];
  }
  rep(i, n) {
    if (num[i])
      G[i].push_back((i + m) % n);
    if (num[i] && i == (i + m) % n) {
      printf("-1\n");
      return 0;
    }
    d[i] = -1;
  }
  auto scc_result = scc();
  if (scc_result.fi < n) {
    printf("-1");
    return 0;
  }
  int ans = 0;
  rep(i, n) ans = max(ans, longest_path(i));
  printf("%d\n", ans);
}