Problem p02962 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define FOR(i, k, n) for (ll i = (k); i < (n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) begin(x), end(x)

using namespace std;
using ll = int64_t;
using vecint = vector<ll>;
constexpr ll MOD = 1000000007;

ll pm(ll x, ll i) {
  if (i == 0)
    return 1;
  if ((i % 2) == 1)
    return (pm(x, i - 1) * x) % MOD;
  ll h = pm(x, i / 2);
  return (h * h) % MOD;
}

int main() {
  string s, t;
  cin >> s >> t;
  int n = s.size();
  int m = t.size();
  stringstream ss;
  REP(i, (m + n - 1) / n + 1) { ss << s; }
  string ks = ss.str();
  ll h = 0, th = 0;
  REP(i, m) {
    h *= 31415;
    h += ks[i];
    h %= MOD;
    th *= 31415;
    th += t[i];
    th %= MOD;
  }
  ll coeff = pm(31415, m);
  vector<bool> v(n, false);
  REP(i, n) {
    if (h == th) {
      v[i] = true;
    }
    h *= 31415;
    h %= MOD;
    h += (MOD - ks[i]) * coeff;
    h %= MOD;
    h += ks[i + m];
    h %= MOD;
  }
  vecint len(n, -1);
  bool ok = false;
  REP(i, n) {
    if (len[i] < 0) {
      if (!v[i]) {
        len[i] = 0;
        continue;
      }
      FOR(j, 1, n) {
        ll idx = (i + j * m) % n;
        if (len[idx] >= 0) {
          REP(k, j) {
            ll idx2 = (i + k * m) % n;
            len[idx2] = len[idx] + (j - k);
          }
          break;
        } else if (!v[idx]) {
          REP(k, j + 1) {
            ll idx2 = (i + k * m) % n;
            len[idx2] = j - k;
          }
          break;
        } else if (idx == i) {
          ok = true;
          break;
        }
      }
      if (ok)
        break;
    }
  }
  if (ok) {
    cout << -1 << endl;
  } else {
    cout << *max_element(ALL(len)) << endl;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define FOR(i, k, n) for (ll i = (k); i < (n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) begin(x), end(x)

using namespace std;
using ll = int64_t;
using vecint = vector<ll>;
constexpr ll MOD = 1000000007;

ll pm(ll x, ll i) {
  if (i == 0)
    return 1;
  if ((i % 2) == 1)
    return (pm(x, i - 1) * x) % MOD;
  ll h = pm(x, i / 2);
  return (h * h) % MOD;
}

int main() {
  string s, t;
  cin >> s >> t;
  int n = s.size();
  int m = t.size();
  stringstream ss;
  REP(i, (m + n - 1) / n + 1) { ss << s; }
  string ks = ss.str();
  ll h = 0, th = 0;
  REP(i, m) {
    h *= 31415;
    h += ks[i];
    h %= MOD;
    th *= 31415;
    th += t[i];
    th %= MOD;
  }
  ll coeff = pm(31415, m);
  vector<bool> v(n, false);
  REP(i, n) {
    if (h == th) {
      v[i] = true;
    }
    h *= 31415;
    h %= MOD;
    h += (MOD - ks[i]) * coeff;
    h %= MOD;
    h += ks[i + m];
    h %= MOD;
  }
  vecint len(n, -1);
  bool ok = false;
  REP(i, n) {
    if (len[i] < 0) {
      if (!v[i]) {
        len[i] = 0;
        continue;
      }
      FOR(j, 1, n + 1) {
        ll idx = (i + j * m) % n;
        if (len[idx] >= 0) {
          REP(k, j) {
            ll idx2 = (i + k * m) % n;
            len[idx2] = len[idx] + (j - k);
          }
          break;
        } else if (!v[idx]) {
          REP(k, j + 1) {
            ll idx2 = (i + k * m) % n;
            len[idx2] = j - k;
          }
          break;
        } else if (idx == i) {
          ok = true;
          break;
        }
      }
      if (ok)
        break;
    }
  }
  if (ok) {
    cout << -1 << endl;
  } else {
    cout << *max_element(ALL(len)) << endl;
  }
  return 0;
}