Problem p02962 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
using ll = long long;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;

vector<int> calc(string &S) {
  vector<int> A(S.size());
  A[0] = S.size();
  int i = 1, j = 0;
  while (i < S.size()) {
    while (i + j < S.size() && S[j] == S[i + j])
      ++j;
    A[i] = j;
    if (j == 0) {
      ++i;
      continue;
    }
    int k = 1;
    while (i + k < S.size() && k + A[k] < j)
      A[i + k] = A[k], ++k;
    i += k;
    j -= k;
  }
  return A;
}
int main() {
  string S, t;
  cin >> S >> t;
  string s = "";
  while (s.size() < t.size())
    s += S;
  s += s;
  s += s;
  int ok = 0, ng = s.size() / t.size() + 1;
  while (ng - ok > 1) {
    int mid = (ok + ng) / 2;
    string u = "";
    rep(i, mid) u += t;
    u += s;
    auto ret = calc(u);
    int ma = 0;
    REP(i, mid * t.size(), ret.size()) { ma = max(ma, ret[i]); }
    if (ma >= mid * t.size())
      ok = mid;
    else
      ng = mid;
  }
  s += s;
  int res = ok;
  ok = 0, ng = s.size() / t.size() + 1;
  while (ng - ok > 1) {
    int mid = (ok + ng) / 2;
    string u = "";
    rep(i, mid) u += t;
    u += s;
    auto ret = calc(u);
    int ma = 0;
    REP(i, mid * t.size(), ret.size()) { ma = max(ma, ret[i]); }
    if (ma >= mid * t.size())
      ok = mid;
    else
      ng = mid;
  }
  if (ok > res) {
    cout << -1 << endl;
  } else
    cout << ok << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
using ll = long long;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;

vector<int> calc(string &S) {
  vector<int> A(S.size());
  A[0] = S.size();
  int i = 1, j = 0;
  while (i < S.size()) {
    while (i + j < S.size() && S[j] == S[i + j])
      ++j;
    A[i] = j;
    if (j == 0) {
      ++i;
      continue;
    }
    int k = 1;
    while (i + k < S.size() && k + A[k] < j)
      A[i + k] = A[k], ++k;
    i += k;
    j -= k;
  }
  return A;
}
int main() {
  string S, t;
  cin >> S >> t;
  string w = "";
  while (w.size() < t.size())
    w += S;
  string s = w + w + w;
  int ok = 0, ng = s.size() / t.size() + 1;
  while (ng - ok > 1) {
    int mid = (ok + ng) / 2;
    string u = "";
    rep(i, mid) u += t;
    u += s;
    auto ret = calc(u);
    int ma = 0;
    REP(i, mid * t.size(), ret.size()) { ma = max(ma, ret[i]); }
    if (ma >= mid * t.size())
      ok = mid;
    else
      ng = mid;
  }
  s += s;
  int res = ok;
  ok = 0, ng = s.size() / t.size() + 1;
  while (ng - ok > 1) {
    int mid = (ok + ng) / 2;
    string u = "";
    rep(i, mid) u += t;
    u += s;
    auto ret = calc(u);
    int ma = 0;
    REP(i, mid * t.size(), ret.size()) { ma = max(ma, ret[i]); }
    if (ma >= mid * t.size())
      ok = mid;
    else
      ng = mid;
  }
  if (ok > res) {
    cout << -1 << endl;
  } else
    cout << ok << endl;
  return 0;
}