Problem p02962 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) begin(v), end(v)
#define fi first
#define se second
template <typename A, typename B> inline bool chmax(A &a, B b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename A, typename B> inline bool chmin(A &a, B b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using ll = long long;
using pii = pair<int, int>;
constexpr ll INF = 1ll << 30;
constexpr ll longINF = 1ll << 60;
constexpr ll MOD = 1000000007;
constexpr bool debug = 0;
//---------------------------------//

vector<int> z_algorithm(string s) {
  vector<int> res(s.size());

  res[0] = s.size();

  int i = 1, j = 0;

  while (i < s.size()) {
    while (i + j < s.size() && s[j] == s[i + j])
      j++;
    res[i] = j;
    if (j == 0) {
      i++;
      continue;
    }
    int k = 1;
    while (i + k < s.size() && k + res[k] < j)
      res[i + k] = res[k], k++;
    i += k;
    j -= k;
  }

  return res;
}

vector<int> g[123456];
int indeg[1123456];
int dp[1123456];

int main() {
  string S, T;
  cin >> S >> T;

  while (T.size() > S.size())
    S += S;

  string str = T + S + S;
  vector<int> v = z_algorithm(str);

  FOR(i, T.size(), T.size() + S.size()) {
    if (v[i] >= T.size()) {
      g[i - T.size()].emplace_back(i % S.size());
      ++indeg[i % S.size()];
    }
  }

  queue<int> que;
  REP(i, S.size()) if (indeg[i] == 0) que.push(i);

  int ans = 0;
  while (!que.empty()) {
    int u = que.front();
    que.pop();
    chmax(ans, dp[u]);

    for (int v : g[u]) {
      chmax(dp[v], dp[u] + 1);
      if (--indeg[v] == 0)
        que.push(v);
    }
  }

  REP(i, S.size()) if (indeg[i]) ans = -1;
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) begin(v), end(v)
#define fi first
#define se second
template <typename A, typename B> inline bool chmax(A &a, B b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename A, typename B> inline bool chmin(A &a, B b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using ll = long long;
using pii = pair<int, int>;
constexpr ll INF = 1ll << 30;
constexpr ll longINF = 1ll << 60;
constexpr ll MOD = 1000000007;
constexpr bool debug = 0;
//---------------------------------//

vector<int> z_algorithm(string s) {
  vector<int> res(s.size());

  res[0] = s.size();

  int i = 1, j = 0;

  while (i < s.size()) {
    while (i + j < s.size() && s[j] == s[i + j])
      j++;
    res[i] = j;
    if (j == 0) {
      i++;
      continue;
    }
    int k = 1;
    while (i + k < s.size() && k + res[k] < j)
      res[i + k] = res[k], k++;
    i += k;
    j -= k;
  }

  return res;
}

vector<int> g[1123456];
int indeg[1123456];
int dp[1123456];

int main() {
  string S, T;
  cin >> S >> T;

  while (T.size() > S.size())
    S += S;

  string str = T + S + S;
  vector<int> v = z_algorithm(str);

  FOR(i, T.size(), T.size() + S.size()) {
    if (v[i] >= T.size()) {
      g[i - T.size()].emplace_back(i % S.size());
      ++indeg[i % S.size()];
    }
  }

  queue<int> que;
  REP(i, S.size()) if (indeg[i] == 0) que.push(i);

  int ans = 0;
  while (!que.empty()) {
    int u = que.front();
    que.pop();
    chmax(ans, dp[u]);

    for (int v : g[u]) {
      chmax(dp[v], dp[u] + 1);
      if (--indeg[v] == 0)
        que.push(v);
    }
  }

  REP(i, S.size()) if (indeg[i]) ans = -1;
  cout << ans << endl;
  return 0;
}