Problem p02962 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
#define repd(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) repd(i, 0, n)
#define all(x) (x).begin(), (x).end()
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
typedef long long ll;
const long long INF = 1LL << 60;
typedef pair<int, int> P;
typedef pair<ll, ll> p;
typedef vector<int> vec;
using Graph = vector<vector<int>>;
using graph = vector<vector<ll>>;

const int inf = 1e9 + 7;
const long long MOD = 1000000007;

// ローリングハッシュ
// 二分探索で LCP を求める機能つき
struct RollingHash {
  static const int base1 = 1007, base2 = 2009;
  static const int mod1 = 1000000007, mod2 = 1000000009;
  vector<long long> hash1, hash2, power1, power2;

  // construct
  RollingHash(const string &S) {
    int n = (int)S.size();
    hash1.assign(n + 1, 0);
    hash2.assign(n + 1, 0);
    power1.assign(n + 1, 1);
    power2.assign(n + 1, 1);
    for (int i = 0; i < n; ++i) {
      hash1[i + 1] = (hash1[i] * base1 + S[i]) % mod1;
      hash2[i + 1] = (hash2[i] * base2 + S[i]) % mod2;
      power1[i + 1] = (power1[i] * base1) % mod1;
      power2[i + 1] = (power2[i] * base2) % mod2;
    }
  }

  // get hash of S[left:right]
  inline pair<long long, long long> get(int l, int r) const {
    long long res1 = hash1[r] - hash1[l] * power1[r - l] % mod1;
    if (res1 < 0)
      res1 += mod1;
    long long res2 = hash2[r] - hash2[l] * power2[r - l] % mod2;
    if (res2 < 0)
      res2 += mod2;
    return {res1, res2};
  }
};

int main() {
  string s, t;
  vec v;
  cin >> s >> t;
  while (s.size() < t.size()) {
    s += s;
  }
  int lent = t.size();
  int lens = s.size();
  s += s + s;
  RollingHash rhs(s), rht(t);
  p Thash = rht.get(0, lent);
  rep(i, lens) {
    if (rhs.get(i, i + lent) == Thash) {
      v.push_back(i);
    }
  }
  if (v.empty())
    cout << 0 << endl;
  else {
    map<int, int> ma;
    int i = 0;
    rep(i, v.size()) { ma[v[i]] = 1; }
    int CNT = 0;
    int a = v[0];
    while (CNT <= 500000) {
      a += lent;
      a %= lens;
      if (ma.count(a))
        CNT++;
      else
        break;
    }
    if (CNT == 500001)
      cout << -1 << endl;
    else {
      map<int, int> MA;
      int ans = 1;
      int cnt = 1;
      rep(i, v.size()) {
        int q = v[i];

        MA[q] = 1;
        while (ma.count(q)) {
          cnt++;
          q += lent;
          q %= lens;
          MA[q] = 1;
        }
        ans = max(ans, cnt - 1);
        cnt = 1;
      }
      cout << ans << endl;
    }
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
#define repd(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) repd(i, 0, n)
#define all(x) (x).begin(), (x).end()
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
typedef long long ll;
const long long INF = 1LL << 60;
typedef pair<int, int> P;
typedef pair<ll, ll> p;
typedef vector<int> vec;
using Graph = vector<vector<int>>;
using graph = vector<vector<ll>>;

const int inf = 1e9 + 7;
const long long MOD = 1000000007;

// ローリングハッシュ
// 二分探索で LCP を求める機能つき
struct RollingHash {
  static const int base1 = 1007, base2 = 2009;
  static const int mod1 = 1000000007, mod2 = 1000000009;
  vector<long long> hash1, hash2, power1, power2;

  // construct
  RollingHash(const string &S) {
    int n = (int)S.size();
    hash1.assign(n + 1, 0);
    hash2.assign(n + 1, 0);
    power1.assign(n + 1, 1);
    power2.assign(n + 1, 1);
    for (int i = 0; i < n; ++i) {
      hash1[i + 1] = (hash1[i] * base1 + S[i]) % mod1;
      hash2[i + 1] = (hash2[i] * base2 + S[i]) % mod2;
      power1[i + 1] = (power1[i] * base1) % mod1;
      power2[i + 1] = (power2[i] * base2) % mod2;
    }
  }

  // get hash of S[left:right]
  inline pair<long long, long long> get(int l, int r) const {
    long long res1 = hash1[r] - hash1[l] * power1[r - l] % mod1;
    if (res1 < 0)
      res1 += mod1;
    long long res2 = hash2[r] - hash2[l] * power2[r - l] % mod2;
    if (res2 < 0)
      res2 += mod2;
    return {res1, res2};
  }
};

int main() {
  string s, t;
  vec v;
  cin >> s >> t;
  while (s.size() < t.size()) {
    s += s;
  }
  int lent = t.size();
  int lens = s.size();
  s += s + s;
  RollingHash rhs(s), rht(t);
  p Thash = rht.get(0, lent);
  rep(i, lens) {
    if (rhs.get(i, i + lent) == Thash) {
      v.push_back(i);
    }
  }
  if (v.empty())
    cout << 0 << endl;
  else {
    map<int, int> ma;
    int i = 0;
    rep(i, v.size()) { ma[v[i]] = 1; }
    int CNT = 0;
    int a = v[0];
    while (CNT <= 500000) {
      a += lent;
      a %= lens;
      if (ma.count(a))
        CNT++;
      else
        break;
    }
    if (CNT == 500001)
      cout << -1 << endl;
    else {
      map<int, int> MA;
      int ans = 1;
      int cnt = 1;
      rep(i, v.size()) {
        int q = v[i];
        if (MA.count(q))
          continue;
        MA[q] = 1;
        while (ma.count(q)) {
          cnt++;
          q += lent;
          q %= lens;
          MA[q] = 1;
        }
        ans = max(ans, cnt - 1);
        cnt = 1;
      }
      cout << ans << endl;
    }
  }

  return 0;
}