Problem p02962 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define F first
#define S second

using namespace std;
typedef long double ld;
typedef long long ll;

const int max_n = 100011, inf = 1000111222;
const ll inff = 1000111222333444;

string s, t;
char ss[max_n];

bool is_cycle(string a, const string &b) {
  // cout << "is_cycle: " << a << ", " << b << endl;
  if (b.length() % a.length() != 0)
    return false;
  int i = 0;
  for (int q = 0; q < b.length(); ++q) {
    if (b[q] != a[i])
      return false;
    ++i;
    if (i == a.length())
      i = 0;
  }
  return true;
}

void get_p(const string &b, vector<int> &p) {
  assert(p.size() == b.length());
  if (b.empty())
    return;
  p[0] = 0;
  for (int i = 1; i < p.size(); ++i) {
    int k = p[i - 1];
    while (k > 0 && b[k] != b[i]) {
      k = p[k - 1];
    }
    if (b[i] == b[k])
      ++k;
    p[i] = k;
  }
}

bool is_eq_cycle(string a, string b) {
  // cout << "is_eq_cycle: " << a << ", " << b << endl;
  if (a.length() != b.length())
    return false;
  b = a + "#" + b + b;
  vector<int> p(b.length());
  get_p(b, p);
  for (int i = 1; i < b.length(); ++i) {
    if (p[i] == a.length()) {
      return true;
    }
  }
  return false;
}

int get_ans(const string &a, const string &b) {
  string s = a + "#" + b + b;
  vector<int> dp(s.length()), p(s.length());
  get_p(s, p);
  int ans = 0;
  // cout << s << endl;
  // for (int i = 0; i < s.length(); ++i) cout << p[i] << " "; cout << endl;
  for (int i = a.length(); i < s.length(); ++i) {
    if (p[i] == a.length()) {
      dp[i] = dp[i - a.length()] + 1;
      ans = max(ans, dp[i]);
    }
  }
  return ans;
}

int main() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  scanf("%s", ss);
  s = ss;
  scanf("%s", ss);
  t = ss;
  int g = __gcd(s.length(), t.length());
  if (is_cycle(s.substr(0, g), s) && is_cycle(t.substr(0, g), t) &&
      is_eq_cycle(s.substr(0, g), t.substr(0, g))) {
    cout << -1 << endl;
    return 0;
  }
  while (s.length() < t.length())
    s = s + s;
  s = s + s;
  cout << get_ans(t, s) << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

#define F first
#define S second

using namespace std;
typedef long double ld;
typedef long long ll;

const int max_n = 500011, inf = 1000111222;
const ll inff = 1000111222333444;

string s, t;
char ss[max_n];

bool is_cycle(string a, const string &b) {
  // cout << "is_cycle: " << a << ", " << b << endl;
  if (b.length() % a.length() != 0)
    return false;
  int i = 0;
  for (int q = 0; q < b.length(); ++q) {
    if (b[q] != a[i])
      return false;
    ++i;
    if (i == a.length())
      i = 0;
  }
  return true;
}

void get_p(const string &b, vector<int> &p) {
  assert(p.size() == b.length());
  if (b.empty())
    return;
  p[0] = 0;
  for (int i = 1; i < p.size(); ++i) {
    int k = p[i - 1];
    while (k > 0 && b[k] != b[i]) {
      k = p[k - 1];
    }
    if (b[i] == b[k])
      ++k;
    p[i] = k;
  }
}

bool is_eq_cycle(string a, string b) {
  // cout << "is_eq_cycle: " << a << ", " << b << endl;
  if (a.length() != b.length())
    return false;
  b = a + "#" + b + b;
  vector<int> p(b.length());
  get_p(b, p);
  for (int i = 1; i < b.length(); ++i) {
    if (p[i] == a.length()) {
      return true;
    }
  }
  return false;
}

int get_ans(const string &a, const string &b) {
  string s = a + "#" + b + b;
  vector<int> dp(s.length()), p(s.length());
  get_p(s, p);
  int ans = 0;
  // cout << s << endl;
  // for (int i = 0; i < s.length(); ++i) cout << p[i] << " "; cout << endl;
  for (int i = a.length(); i < s.length(); ++i) {
    if (p[i] == a.length()) {
      dp[i] = dp[i - a.length()] + 1;
      ans = max(ans, dp[i]);
    }
  }
  return ans;
}

int main() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  scanf("%s", ss);
  s = ss;
  scanf("%s", ss);
  t = ss;
  int g = __gcd(s.length(), t.length());
  if (is_cycle(s.substr(0, g), s) && is_cycle(t.substr(0, g), t) &&
      is_eq_cycle(s.substr(0, g), t.substr(0, g))) {
    cout << -1 << endl;
    return 0;
  }
  while (s.length() < t.length())
    s = s + s;
  s = s + s;
  cout << get_ans(t, s) << endl;
  return 0;
}