Problem p02962 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
#define FOR(i, begin, end)                                                     \
  for (int i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define IFOR(i, begin, end)                                                    \
  for (int i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
constexpr int MOD = 1000000007;
int main() {
  string s, t;
  cin >> s >> t;
  string sloop = "";
  while (sloop.size() <= s.size() + t.size()) {
    sloop += s;
  }
  vector<int> table(t.size(), 0);
  int i = 2;
  int j = 0;
  table[0] = -1;
  if (t.size() >= 2)
    table[1] = 0;
  while (i < t.size()) {
    if (t[i - 1] == t[j]) {
      table[i] = j + 1;
      i++;
      j++;
    } else if (j > 0) {
      j = table[j];
    } else {
      table[i] = 0;
      i++;
    }
  }
  int m = 0;
  i = 0;
  vector<bool> pat(s.size(), false);
  while (m + i < sloop.size()) {
    if (t[i] == sloop[m + i]) {
      i++;
      if (i == t.size()) {
        pat[m] = true;
        i--;
        m = m + i - table[i];
        if (i > 0) {
          i = table[i];
        }
      }
    } else {
      m = m + i - table[i];
      if (i > 0) {
        i = table[i];
      }
    }
  }
  int ans = 0;
  vector<bool> used(s.size(), false);
  int smul = 0;
  while (smul < t.size()) {
    smul += s.size();
  }
  REP(i, s.size()) {
    if (pat[i] == true && used[i] == false) {
      int j = i;
      while (true) {
        if (pat[(j - t.size() + smul) % s.size()] == true) {
          j = (j - t.size() + smul) % s.size();
          if (i == j) {
            cout << -1 << endl;
            return 0;
          }
        } else {
          break;
        }
      }
      int k = 0;
      while (true) {
        used[j] = true;
        k++;
        if (pat[(j + t.size()) % s.size()] == true) {
          j = (j + t.size()) % s.size();
        } else {
          break;
        }
      }
      if (k > ans)
        ans = k;
    }
  }

  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
#define FOR(i, begin, end)                                                     \
  for (int i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define IFOR(i, begin, end)                                                    \
  for (int i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
constexpr int MOD = 1000000007;
int main() {
  string s, t;
  cin >> s >> t;
  string sloop = "";
  while (sloop.size() <= s.size() + t.size()) {
    sloop += s;
  }
  vector<int> table(t.size(), 0);
  int i = 2;
  int j = 0;
  table[0] = -1;
  if (t.size() >= 2)
    table[1] = 0;
  while (i < t.size()) {
    if (t[i - 1] == t[j]) {
      table[i] = j + 1;
      i++;
      j++;
    } else if (j > 0) {
      j = table[j];
    } else {
      table[i] = 0;
      i++;
    }
  }
  int m = 0;
  i = 0;
  vector<bool> pat(s.size(), false);
  while (m < s.size()) {
    if (t[i] == sloop[m + i]) {
      i++;
      if (i == t.size()) {
        pat[m] = true;
        i--;
        m = m + i - table[i];
        if (i > 0) {
          i = table[i];
        }
      }
    } else {
      m = m + i - table[i];
      if (i > 0) {
        i = table[i];
      }
    }
  }
  int ans = 0;
  vector<bool> used(s.size(), false);
  int smul = 0;
  while (smul < t.size()) {
    smul += s.size();
  }
  REP(i, s.size()) {
    if (pat[i] == true && used[i] == false) {
      int j = i;
      while (true) {
        if (pat[(j - t.size() + smul) % s.size()] == true) {
          j = (j - t.size() + smul) % s.size();
          if (i == j) {
            cout << -1 << endl;
            return 0;
          }
        } else {
          break;
        }
      }
      int k = 0;
      while (true) {
        used[j] = true;
        k++;
        if (pat[(j + t.size()) % s.size()] == true) {
          j = (j + t.size()) % s.size();
        } else {
          break;
        }
      }
      if (k > ans)
        ans = k;
    }
  }

  cout << ans << endl;
}