Problem p02962 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <ciso646>
#include <cmath>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop                                                                   \
  char nyaa;                                                                   \
  cin >> nyaa;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per1(i, n) for (int i = n; i >= 1; i--)
#define Rep1(i, sta, n) for (int i = sta; i <= n; i++)
typedef long double ld;
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;

struct rolling_hash {
private:
  ll b = 10007;
  vector<vector<ll>> hash;
  int MAX_L = 100000;
  vector<ll> b_pow;

public:
  void init() {
    hash = {};
    b_pow = {1};
    rep(i, MAX_L) {
      b_pow.push_back(b_pow.back() * b % mod);
      // cout << b_pow.back() << endl;
    }
  }

  void make_hash(string s) {
    int l = s.length();
    vector<ll> h;
    h.push_back(0);
    rep(i, l) {
      h.push_back((h.back() * b % mod + s[i]) % mod);
      // cout << i << " " << h.back() << endl;
    }
    hash.push_back(h);
  }

  bool same(int a1, P d1, int a2, P d2) {
    int l1 = d1.first, r1 = d1.second, l2 = d2.first, r2 = d2.second;
    if (r1 - l1 != r2 - l2)
      return false;
    ll h1 = (hash[a1][r1] - b_pow[r1 - l1] * hash[a1][l1] % mod) % mod,
       h2 = (hash[a2][r2] - b_pow[r2 - l2] * hash[a2][l2] % mod) % mod;
    if (h1 < 0)
      h1 += mod;
    if (h2 < 0)
      h2 += mod;
    // cout << h1 << " " << h2 << endl;
    if (h1 == h2)
      return true;
    return false;
  }
};

string s_, s = "", t;
vector<bool> S = {}, used = {};
vector<ll> d;
int ls, lt;

ll c(int m, int sta, bool first) {
  // cout << m << " " << sta << " " << first << endl;
  if (!S[m])
    return 0;
  if (!first && m == sta) {
    cout << -1 << endl;
    exit(0);
  }
  if (used[m])
    return d[m];
  used[m] = true;
  ll k = c((m + lt) % ls, sta, false) + 1;
  d[m] = k;
  return k;
}

void solve() {
  cin >> s_;
  cin >> t;
  while (s.length() < t.length()) {
    s += s_;
  }
  // cout << s << " " << t << endl;
  ls = s.length(), lt = t.length();
  // cout << ls << " " << lt << endl;
  while (s.length() - ls < lt) {
    s += s_;
  }
  // cout << s << " " << t << endl;
  rolling_hash h;
  h.init();
  h.make_hash(s);
  h.make_hash(t);
  rep(i, ls) {
    // cout << s.substr(i,lt) << " " << t << endl;
    S.push_back(h.same(0, P(i, i + lt), 1, P(0, lt)));
    used.push_back(false);
    d.push_back(0);
  }
  ll ans = 0;
  rep(i, ls) {
    if (!used[i])
      c(i, i, true);
    // cout << d[i] << " " << S[i] << endl;
    ans = max(ans, d[i]);
  }
  cout << ans << endl;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  solve();
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <ciso646>
#include <cmath>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop                                                                   \
  char nyaa;                                                                   \
  cin >> nyaa;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per1(i, n) for (int i = n; i >= 1; i--)
#define Rep1(i, sta, n) for (int i = sta; i <= n; i++)
typedef long double ld;
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;

struct rolling_hash {
private:
  ll b = 10007;
  vector<vector<ll>> hash;
  int MAX_L = 5000000;
  vector<ll> b_pow;

public:
  void init() {
    hash = {};
    b_pow = {1};
    rep(i, MAX_L) {
      b_pow.push_back(b_pow.back() * b % mod);
      // cout << b_pow.back() << endl;
    }
  }

  void make_hash(string s) {
    int l = s.length();
    vector<ll> h;
    h.push_back(0);
    rep(i, l) {
      h.push_back((h.back() * b % mod + s[i]) % mod);
      // cout << i << " " << h.back() << endl;
    }
    hash.push_back(h);
  }

  bool same(int a1, P d1, int a2, P d2) {
    int l1 = d1.first, r1 = d1.second, l2 = d2.first, r2 = d2.second;
    if (r1 - l1 != r2 - l2)
      return false;
    ll h1 = (hash[a1][r1] - b_pow[r1 - l1] * hash[a1][l1] % mod) % mod,
       h2 = (hash[a2][r2] - b_pow[r2 - l2] * hash[a2][l2] % mod) % mod;
    if (h1 < 0)
      h1 += mod;
    if (h2 < 0)
      h2 += mod;
    // cout << h1 << " " << h2 << endl;
    if (h1 == h2)
      return true;
    return false;
  }
};

string s_, s = "", t;
vector<bool> S = {}, used = {};
vector<ll> d;
int ls, lt;

ll c(int m, int sta, bool first) {
  // cout << m << " " << sta << " " << first << endl;
  if (!S[m])
    return 0;
  if (!first && m == sta) {
    cout << -1 << endl;
    exit(0);
  }
  if (used[m])
    return d[m];
  used[m] = true;
  ll k = c((m + lt) % ls, sta, false) + 1;
  d[m] = k;
  return k;
}

void solve() {
  cin >> s_;
  cin >> t;
  while (s.length() < t.length()) {
    s += s_;
  }
  // cout << s << " " << t << endl;
  ls = s.length(), lt = t.length();
  // cout << ls << " " << lt << endl;
  while (s.length() - ls < lt) {
    s += s_;
  }
  // cout << s << " " << t << endl;
  rolling_hash h;
  h.init();
  h.make_hash(s);
  h.make_hash(t);
  rep(i, ls) {
    // cout << s.substr(i,lt) << " " << t << endl;
    S.push_back(h.same(0, P(i, i + lt), 1, P(0, lt)));
    used.push_back(false);
    d.push_back(0);
  }
  ll ans = 0;
  rep(i, ls) {
    if (!used[i])
      c(i, i, true);
    // cout << d[i] << " " << S[i] << endl;
    ans = max(ans, d[i]);
  }
  cout << ans << endl;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  solve();
}