Problem p02962 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define eb emplace_back

typedef unsigned long long ll;
typedef long double ld;

const int IN_PROCESS = -2;
const int NOT_DONE = -1;
const int INF = 1e9;

template <class T> struct segtree_t {
  vector<T> t;
  int n;

  segtree_t(const vector<T> &elems) {
    n = elems.size();
    t = vector<T>(2 * n);
    for (int i = 0; i < n; i++) {
      t[n + i] = elems[i];
    }
    build();
  }

  void build() { // build the tree
    for (int i = n - 1; i > 0; --i)
      t[i] = min(t[i << 1], t[i << 1 | 1]);
  }

  void modify(int p, T value) { // set value at position p
    for (t[p += n] = value; p > 1; p >>= 1)
      t[p >> 1] = min(t[p], t[p ^ 1]);
  }

  T query(int l, int r) { // sum on interval [l, r)
    int res = INF;
    for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
      if (l & 1)
        res = min(res, t[l++]);
      if (r & 1)
        res = min(res, t[--r]);
    }
    return res;
  }
};

int calc(vector<int> &cyclic, int idx, const vector<char> &T, const int step,
         const char expected) {
  // cout << "IDX " << idx << endl;
  if (T[idx] != expected) {
    return 0;
  }

  if (cyclic[idx] == IN_PROCESS) {
    cyclic[idx] = INF;
    return INF;
  }

  cyclic[idx] = IN_PROCESS;
  int val = calc(cyclic, (idx + step) % T.size(), T, step, expected);
  if (val != INF) {
    val++;
  }
  cyclic[idx] = val;
  return val;
}

vector<int> prefix_function(vector<char> &s) {
  int n = (int)s.size();
  vector<int> pi(n);
  for (int i = 1; i < n; i++) {
    int j = pi[i - 1];
    while (j > 0 && s[i] != s[j])
      j = pi[j - 1];
    if (s[i] == s[j])
      j++;
    pi[i] = j;
  }
  return pi;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  vector<char> S, T;

  string a, b;
  cin >> a >> b;

  S.reserve(b.length());
  for (char e : b) {
    S.pb(e);
  }

  unsigned int sz = 3 * max(a.length(), S.size());
  T.reserve(sz + a.length());
  while (T.size() < sz) {
    for (char e : a) {
      T.pb(e);
    }
  }

  vector<int> cyclic(T.size(), NOT_DONE);
  int n = S.size();
  for (int i = 0; i < (int)cyclic.size(); i++) {
    if (cyclic[i] == NOT_DONE) {
      calc(cyclic, i, T, n, T[i]);
    }
    // cout << cyclic[i] << "\t";
  }

  segtree_t<int> seg(cyclic);

  vector<int> pref_size = prefix_function(S);

  int res = 0;

  int le = 0;
  for (int i = 0; i < (int)T.size(); i++) {
    if (S[le] == T[i]) {
      le++;
      if (le == n) {
        // cout << "MATCH" << endl;
        res = max(res, seg.query(i - (n - 1), i + 1));
        le = pref_size[n - 1];
      }
    } else {
      while (le > 0 && S[le] != T[i]) {
        le = pref_size[le - 1];
      }
      if (S[le] == T[i]) {
        le++;
      }
    }
  }
  if (res == INF) {
    res = -1;
  }
  cout << res << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define eb emplace_back

typedef unsigned long long ll;
typedef long double ld;

const int IN_PROCESS = -2;
const int NOT_DONE = -1;
const int INF = 1e9;

template <class T> struct segtree_t {
  vector<T> t;
  int n;

  segtree_t(const vector<T> &elems) {
    n = elems.size();
    t = vector<T>(2 * n);
    for (int i = 0; i < n; i++) {
      t[n + i] = elems[i];
    }
    build();
  }

  void build() { // build the tree
    for (int i = n - 1; i > 0; --i)
      t[i] = min(t[i << 1], t[i << 1 | 1]);
  }

  void modify(int p, T value) { // set value at position p
    for (t[p += n] = value; p > 1; p >>= 1)
      t[p >> 1] = min(t[p], t[p ^ 1]);
  }

  T query(int l, int r) { // sum on interval [l, r)
    int res = INF;
    for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
      if (l & 1)
        res = min(res, t[l++]);
      if (r & 1)
        res = min(res, t[--r]);
    }
    return res;
  }
};

int calc(vector<int> &cyclic, int idx, const vector<char> &T, const int step,
         const char expected) {
  // cout << "IDX " << idx << endl;
  if (T[idx] != expected) {
    return 0;
  }

  if (cyclic[idx] > 0) {
    return cyclic[idx];
  }

  if (cyclic[idx] == IN_PROCESS) {
    cyclic[idx] = INF;
    return INF;
  }

  cyclic[idx] = IN_PROCESS;
  int val = calc(cyclic, (idx + step) % T.size(), T, step, expected);
  if (val != INF) {
    val++;
  }
  cyclic[idx] = val;
  return val;
}

vector<int> prefix_function(vector<char> &s) {
  int n = (int)s.size();
  vector<int> pi(n);
  for (int i = 1; i < n; i++) {
    int j = pi[i - 1];
    while (j > 0 && s[i] != s[j])
      j = pi[j - 1];
    if (s[i] == s[j])
      j++;
    pi[i] = j;
  }
  return pi;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  vector<char> S, T;

  string a, b;
  cin >> a >> b;

  S.reserve(b.length());
  for (char e : b) {
    S.pb(e);
  }

  unsigned int sz = 3 * max(a.length(), S.size());
  T.reserve(sz + a.length());
  while (T.size() < sz) {
    for (char e : a) {
      T.pb(e);
    }
  }

  vector<int> cyclic(T.size(), NOT_DONE);
  int n = S.size();
  for (int i = 0; i < (int)cyclic.size(); i++) {
    if (cyclic[i] == NOT_DONE) {
      calc(cyclic, i, T, n, T[i]);
    }
    // cout << cyclic[i] << "\t";
  }

  segtree_t<int> seg(cyclic);

  vector<int> pref_size = prefix_function(S);

  int res = 0;

  int le = 0;
  for (int i = 0; i < (int)T.size(); i++) {
    if (S[le] == T[i]) {
      le++;
      if (le == n) {
        // cout << "MATCH" << endl;
        res = max(res, seg.query(i - (n - 1), i + 1));
        le = pref_size[n - 1];
      }
    } else {
      while (le > 0 && S[le] != T[i]) {
        le = pref_size[le - 1];
      }
      if (S[le] == T[i]) {
        le++;
      }
    }
  }
  if (res == INF) {
    res = -1;
  }
  cout << res << endl;
}