Problem p02962 (C++)

Buggy submission (status: Runtime Error):
// #include "bits/stdc++.h"

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define rep(i, a, b) for (int i = (a), i##_len = (b); i < i##_len; i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(c) begin(c), end(c)

#define int ll
#define SZ(x) ((int)(x).size())
#define pb push_back
#define mp make_pair

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<double, double> pdd;
typedef vector<vector<int>> mat;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

const int INF =
    sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;

class KMP {
public:
  string pattern;
  int plen;
  vector<int> table;
  KMP(const string &s)
      : pattern(s), plen((int)pattern.size()), table(plen + 1) {
    table[0] = -1;
    int j = -1;
    for (int i = 0; i < plen; i++) {
      while (j >= 0 && pattern[i] != pattern[j])
        j = table[j];
      if (pattern[i + 1] == pattern[++j])
        table[i + 1] = table[j];
      else
        table[i + 1] = j;
    }
  }
  void search(const string &text, vector<int> &res) {
    int head = 0, j = 0, tlen = (int)text.size();
    while (head + j < tlen) {
      if (pattern[j] == text[head + j]) {
        if (++j != plen)
          continue;
        res.push_back(head);
      }
      head += j - table[j], j = max(table[j], 0LL);
    }
  }
};

bool ok[500010], checked[500010];
int score[500010];

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  string s, t;

  cin >> s >> t;

  string ts;
  while (SZ(ts) < SZ(t) + SZ(s) - 1) {
    ts += s;
  }

  vector<int> mps;
  KMP kmp(t);
  kmp.search(ts, mps);

  for (auto &p : mps) {
    if (SZ(s) <= p)
      break;
    ok[p] = true;
    // cout << p << endl;
  }

  int ans = 0;
  rep(i, 0, SZ(ts)) {
    if (checked[i])
      continue;
    checked[i] = true;
    int p = i;
    int res = 0;
    while (ok[p]) {
      res++;
      p = (p + SZ(t)) % SZ(s);

      if (p == i) {
        res = INF;
        break;
      }
      if (score[p] != 0) {
        res += score[p];
        break;
      }
      checked[p] = true;
    }
    chmax(ans, res);
    score[i] = res;
  }

  if (ans == INF) {
    cout << -1 << endl;
  } else {
    cout << ans << endl;
  }

  return 0;
}

Fixed submission:
// #include "bits/stdc++.h"

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define rep(i, a, b) for (int i = (a), i##_len = (b); i < i##_len; i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(c) begin(c), end(c)

#define int ll
#define SZ(x) ((int)(x).size())
#define pb push_back
#define mp make_pair

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<double, double> pdd;
typedef vector<vector<int>> mat;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

const int INF =
    sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;

class KMP {
public:
  string pattern;
  int plen;
  vector<int> table;
  KMP(const string &s)
      : pattern(s), plen((int)pattern.size()), table(plen + 1) {
    table[0] = -1;
    int j = -1;
    for (int i = 0; i < plen; i++) {
      while (j >= 0 && pattern[i] != pattern[j])
        j = table[j];
      if (pattern[i + 1] == pattern[++j])
        table[i + 1] = table[j];
      else
        table[i + 1] = j;
    }
  }
  void search(const string &text, vector<int> &res) {
    int head = 0, j = 0, tlen = (int)text.size();
    while (head + j < tlen) {
      if (pattern[j] == text[head + j]) {
        if (++j != plen)
          continue;
        res.push_back(head);
      }
      head += j - table[j], j = max(table[j], 0LL);
    }
  }
};

bool ok[500010], checked[500010];
int score[500010];

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  string s, t;

  cin >> s >> t;

  string ts;
  while (SZ(ts) < SZ(t) + SZ(s) - 1) {
    ts += s;
  }

  vector<int> mps;
  KMP kmp(t);
  kmp.search(ts, mps);

  for (auto &p : mps) {
    if (SZ(s) <= p)
      break;
    ok[p] = true;
    // cout << p << endl;
  }

  int ans = 0;
  rep(i, 0, SZ(s)) {
    if (checked[i])
      continue;
    checked[i] = true;
    int p = i;
    int res = 0;
    while (ok[p]) {
      res++;
      p = (p + SZ(t)) % SZ(s);

      if (p == i) {
        res = INF;
        break;
      }
      if (score[p] != 0) {
        res += score[p];
        break;
      }
      checked[p] = true;
    }
    chmax(ans, res);
    score[i] = res;
  }

  if (ans == INF) {
    cout << -1 << endl;
  } else {
    cout << ans << endl;
  }

  return 0;
}