Problem p02962 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pair<int, int>>
#define mp make_pair
#define FOR(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR1(i, n) for (int i = 1; i <= (int)(n); i++)
#define FORD0(i, n) for (int i = (int)n; i >= 0; i--)
#define FORD1(i, n) for (int i = (int)n; i >= 1; i--)
#define debug(X)                                                               \
  { cout << #X << " = " << (X) << endl; }
#define endl '\n'
#define int long long int
using namespace std;
vector<int> lps;
void lpsComp(string pat) {
  int len = 0;
  lps.resize(pat.size());
  lps[0] = 0;

  int i = 1;
  while (i < pat.size()) {
    if (pat[i] == pat[len]) {
      lps[i] = ++len;
      i++;
    } else // (pat[i] != pat[len])
    {
      if (len != 0) {
        len = lps[len - 1];
      } else // if (len == 0)
      {
        lps[i] = 0;
        i++;
      }
    }
  }
}
const int lim = (int)6e5;
bool match[lim] = {0};
int len[lim] = {0};
int vis[lim] = {0};

string s, t;
int best = 0;
bool cycle = false;
// 0 is unvisited, 1 is currently visited, 2 is retired
int dfs(int i) {
  if (vis[i] == 2)
    return len[i];
  if (vis[i] == 1) {
    cycle = true;
    return len[i];
  }
  vis[i] = 1;
  int nex = (i + t.length()) % s.length();
  int ans = 1;
  if (match[nex]) {
    ans += dfs(nex);
  }
  vis[i] = 2;
  len[i] = ans;
  best = max(ans, best);
  return ans;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  // freopen("in", "r", stdin);
  cin >> s >> t;
  lpsComp(t);
  // matching loop
  int pre = 0; // matching at pre th position
  int cnt = 0;
  for (int i = 0; cnt < 1e9; cnt++, i++, i %= s.length()) {
    //    cout << pre <<endl;
    //        if(i == s.length())break;
    if (t[pre] == s[i]) {
      pre++;
    } else if (pre != 0) {
      pre = lps[pre - 1];
      i--;
    }
    if (pre == t.length()) {
      // i th position is the ending
      int start = (i - (int)t.length() + 1);
      start %= (int)s.length();
      while (start < 0)
        start += s.length();
      if (match[start])
        break;
      match[start] = true;
      //       debug(start + t.length());
      pre = lps[pre - 1];
    }
  }
  FOR(i, s.length()) {
    if (match[i] && !vis[i]) {
      dfs(i);
    }
  }
  if (cycle)
    cout << -1;
  else
    cout << best;
}


Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pair<int, int>>
#define mp make_pair
#define FOR(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR1(i, n) for (int i = 1; i <= (int)(n); i++)
#define FORD0(i, n) for (int i = (int)n; i >= 0; i--)
#define FORD1(i, n) for (int i = (int)n; i >= 1; i--)
#define debug(X)                                                               \
  { cout << #X << " = " << (X) << endl; }
#define endl '\n'
#define int long long int
using namespace std;
vector<int> lps;
void lpsComp(string pat) {
  int len = 0;
  lps.resize(pat.size());
  lps[0] = 0;

  int i = 1;
  while (i < pat.size()) {
    if (pat[i] == pat[len]) {
      lps[i] = ++len;
      i++;
    } else // (pat[i] != pat[len])
    {
      if (len != 0) {
        len = lps[len - 1];
      } else // if (len == 0)
      {
        lps[i] = 0;
        i++;
      }
    }
  }
}
const int lim = (int)6e5;
bool match[lim] = {0};
int len[lim] = {0};
int vis[lim] = {0};

string s, t;
int best = 0;
bool cycle = false;
// 0 is unvisited, 1 is currently visited, 2 is retired
int dfs(int i) {
  if (vis[i] == 2)
    return len[i];
  if (vis[i] == 1) {
    cycle = true;
    return len[i];
  }
  vis[i] = 1;
  int nex = (i + t.length()) % s.length();
  int ans = 1;
  if (match[nex]) {
    ans += dfs(nex);
  }
  vis[i] = 2;
  len[i] = ans;
  best = max(ans, best);
  return ans;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  // freopen("in", "r", stdin);
  cin >> s >> t;
  lpsComp(t);
  // matching loop
  int pre = 0; // matching at pre th position
  int cnt = 0;
  for (int i = 0; cnt < (s.length() + t.length()) * 2;
       cnt++, i++, i %= s.length()) {
    //    cout << pre <<endl;
    //        if(i == s.length())break;
    if (t[pre] == s[i]) {
      pre++;
    } else if (pre != 0) {
      pre = lps[pre - 1];
      i--;
    }
    if (pre == t.length()) {
      // i th position is the ending
      int start = (i - (int)t.length() + 1);
      start %= (int)s.length();
      while (start < 0)
        start += s.length();
      if (match[start])
        break;
      match[start] = true;
      //       debug(start + t.length());
      pre = lps[pre - 1];
    }
  }
  FOR(i, s.length()) {
    if (match[i] && !vis[i]) {
      dfs(i);
    }
  }
  if (cycle)
    cout << -1;
  else
    cout << best;
}