Problem p02962 (C++)

Buggy submission (status: Time Limit Exceeded):
/*
 * Welcome to my code!
 *---------------------------------------------*
 * author : lynmisakura(twitter : @andoreiji11)
 */

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define repn for (int i = 0; i < n; i++)
#define ain(a)                                                                 \
  for (auto &i : a)                                                            \
    cin >> i;
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x.size())
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define cont continue

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using pi = pair<int, int>;
using vpi = vector<pi>;
using pl = pair<ll, ll>;
using vpl = vector<pl>;

template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  } else
    return false;
}
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  } else
    return false;
}
template <class T> void print(std::vector<T> a) {
  int sz = a.size();
  for (int i = 0; i < a.size(); i++)
    cout << a[i] << (i < sz - 1 ? ' ' : '\n');
}
void ioboost() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);
}
///////////////////////////////////////////////////////////
void getZarr(string str, int Z[]) {
  int n = str.length();
  int L, R, k;
  L = R = 0;
  for (int i = 1; i < n; ++i) {
    if (i > R) {
      L = R = i;
      while (R < n && str[R - L] == str[R])
        R++;
      Z[i] = R - L;
      R--;
    } else {
      k = i - L;
      if (Z[k] < R - i + 1)
        Z[i] = Z[k];
      else {
        L = i;
        while (R < n && str[R - L] == str[R])
          R++;
        Z[i] = R - L;
        R--;
      }
    }
  }
}
vector<int> search(string text, string pattern) {
  string concat = pattern + "$" + text;
  int l = concat.length();
  int Z[l];
  getZarr(concat, Z);
  vector<int> res;
  for (int i = 0; i < l; ++i)
    if (Z[i] == pattern.length())
      res.push_back(i - pattern.length() - 1);
  return res;
}

int main(int argc, char const *argv[]) {
  ioboost();

  string s, t;
  cin >> s >> t;
  string x = s;
  int n = s.size(), m = t.size();

  rep(i, max(2, (m + n - 1) / n)) s += x;
  auto v = search(s, t);

  vector<int> ok(n, 0);
  for (auto i : v) {
    ok[i % n] = 1;
  }

  vector<int> vis(n, 0);
  int ans = 0;
  rep(i, n) {
    if (vis[i] || !ok[i])
      continue;
    int cnt = 0;
    int now = i;
    while (cnt <= n + 1) {
      if (ok[now]) {
        now = (now + m) % n;
        cnt++;
      } else {
        break;
      }
    }
    if (cnt > n) {
      cout << -1 << '\n';
      return 0;
    } else
      chmax(ans, cnt);
  }
  cout << ans << '\n';
  return 0;
}

Fixed submission:
/*
 * Welcome to my code!
 *---------------------------------------------*
 * author : lynmisakura(twitter : @andoreiji11)
 */

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define repn for (int i = 0; i < n; i++)
#define ain(a)                                                                 \
  for (auto &i : a)                                                            \
    cin >> i;
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x.size())
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define cont continue

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using pi = pair<int, int>;
using vpi = vector<pi>;
using pl = pair<ll, ll>;
using vpl = vector<pl>;

template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  } else
    return false;
}
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  } else
    return false;
}
template <class T> void print(std::vector<T> a) {
  int sz = a.size();
  for (int i = 0; i < a.size(); i++)
    cout << a[i] << (i < sz - 1 ? ' ' : '\n');
}
void ioboost() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);
}
///////////////////////////////////////////////////////////
void getZarr(string str, int Z[]) {
  int n = str.length();
  int L, R, k;
  L = R = 0;
  for (int i = 1; i < n; ++i) {
    if (i > R) {
      L = R = i;
      while (R < n && str[R - L] == str[R])
        R++;
      Z[i] = R - L;
      R--;
    } else {
      k = i - L;
      if (Z[k] < R - i + 1)
        Z[i] = Z[k];
      else {
        L = i;
        while (R < n && str[R - L] == str[R])
          R++;
        Z[i] = R - L;
        R--;
      }
    }
  }
}
vector<int> search(string text, string pattern) {
  string concat = pattern + "$" + text;
  int l = concat.length();
  int Z[l];
  getZarr(concat, Z);
  vector<int> res;
  for (int i = 0; i < l; ++i)
    if (Z[i] == pattern.length())
      res.push_back(i - pattern.length() - 1);
  return res;
}

int main(int argc, char const *argv[]) {
  ioboost();

  string s, t;
  cin >> s >> t;
  string x = s;
  int n = s.size(), m = t.size();

  rep(i, max(2, (m + n - 1) / n)) s += x;
  auto v = search(s, t);

  vector<int> ok(n, 0);
  for (auto i : v) {
    ok[i % n] = 1;
  }

  vector<int> vis(n, 0);
  int ans = 0;
  rep(i, n) {
    if (vis[i] || !ok[i])
      continue;
    int cnt = 0;
    int now = i;
    while (cnt <= n + 1) {
      vis[now]++;
      if (ok[now]) {
        now = (now + m) % n;
        cnt++;
      } else {
        break;
      }
    }
    if (cnt > n) {
      cout << -1 << '\n';
      return 0;
    } else
      chmax(ans, cnt);
  }
  cout << ans << '\n';
  return 0;
}