Problem p02962 (C++)

Buggy submission (status: Runtime Error):
/*
author: Apoorv Singh
*/

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

typedef vector<long long> vi;
typedef pair<long long, long long> pii;
#define pb push_back
#define all(c) c.begin(), c.end()
#define FOR(i, a, b) for (long long i = a; i < b; ++i)
#define FORR(i, a, b) for (long long i = a; i > b; --i)
#define um unordered_map
#define F first
#define S second
#define int long long
#define inf LLONG_MAX
#define endl "\n"
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define _CRT_SECURE_NO_WARNINGS

#define TRACE
#ifndef ONLINE_JUDGE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif

template <class X> void printarr(X arr[], int n) {
  for (int i = 0; i < n; ++i)
    cout << arr[i] << " ";
  cout << endl;
}
template <typename X> ostream &operator<<(ostream &x, const vector<X> &v) {
  for (X a : v)
    x << a << ' ';
  return x;
}
template <typename X, typename Y>
ostream &operator<<(ostream &x, const vector<pair<X, Y>> &v) {
  for (pair<X, Y> it : v)
    x << it.first << ' ' << it.second << endl;
  return x;
}
template <typename T, typename S>
ostream &operator<<(ostream &os, const map<T, S> &v) {
  for (pair<T, S> it : v)
    os << it.first << " => " << it.second << endl;
  return os;
}

double pi = 3.14159265358979323846264338327950288419716939937510582097494459230;
int modulo = 1e9 + 7;

int fpow(int a, int n) {
  int ans = 1;
  while (n) {
    if (n & 1)
      ans = (ans * a) % modulo;
    a = (a * a) % modulo;
    n = n >> 1;
  }
  return ans;
}

/*
const int M = 1e6 + 10;
int composite[M] = {0};

void sieve()
{
    FOR(i,2,M)
    {
        if (!composite[i])
        {
            for (int j = 2*i; j < M; j += i)
                composite[j] = max(composite[j], i);
        }
    }
}
*/

const int M = 5e5 + 10;
vi graph[M], sorted;
int valid[M] = {0}, n, m, visited[M] = {0};

void stringMatching(string s, string t) {
  string a = t + '#' + s;
  int n = a.size();
  int z[n];
  z[0] = 0;

  int l = 0, r = 0;
  FOR(i, 1, n) {
    if (i > r) {
      l = r = i;
      while (r < n and a[r] == a[r - l])
        ++r;
      z[i] = r - l;
      --r;
    }

    else {
      if (z[i - l] < r - i + 1)
        z[i] = z[i - l];

      else {
        l = i;
        while (r < n and a[r - l] == a[r])
          ++r;
        z[i] = r - l;
        --r;
      }
    }
  }

  FOR(i, 0, n) {
    if (z[i] == t.size())
      valid[i - t.size() - 1] = 1;
  }
}

bool isCycle(int curr, int code) {
  visited[curr] = code;
  int next = (curr + m) % n;
  if (not valid[next]) {
    sorted.pb(curr);
    return false;
  }

  if (visited[next] == code)
    return true;
  if (visited[next] == 0 and isCycle(next, code))
    return true;

  sorted.pb(curr);
  return false;
}

int dfs(int curr, int len) {
  visited[curr] = 1;
  int next = (curr + m) % n;
  if (not valid[next])
    return len;

  return dfs(next, len + 1);
}

int solve(string x, string y) {
  n = x.size();
  m = y.size();
  int req = n + m;
  string t = y, s = x;

  while (s.size() < req)
    s += x;
  stringMatching(s, t);

  int code = 1;
  FOR(i, 0, n) {
    if (valid[i] and visited[i] == 0) {
      if (isCycle(i, code))
        return -1;
      ++code;
    }
  }
  if (sorted.size() == 0)
    return 0;

  int ans = 0;
  FOR(i, 0, n) visited[i] = 0;
  FORR(i, sorted.size() - 1, -1) {
    int curr = sorted[i];
    if (valid[curr] and not visited[curr])
      ans = max(ans, dfs(curr, 1));
  }

  return ans;
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  string s, t;
  cin >> s >> t;
  cout << solve(s, t);
  return 0;
}

Fixed submission:
/*
author: Apoorv Singh
*/

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

typedef vector<long long> vi;
typedef pair<long long, long long> pii;
#define pb push_back
#define all(c) c.begin(), c.end()
#define FOR(i, a, b) for (long long i = a; i < b; ++i)
#define FORR(i, a, b) for (long long i = a; i > b; --i)
#define um unordered_map
#define F first
#define S second
#define int long long
#define inf LLONG_MAX
#define endl "\n"
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define _CRT_SECURE_NO_WARNINGS

#define TRACE
#ifndef ONLINE_JUDGE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif

template <class X> void printarr(X arr[], int n) {
  for (int i = 0; i < n; ++i)
    cout << arr[i] << " ";
  cout << endl;
}
template <typename X> ostream &operator<<(ostream &x, const vector<X> &v) {
  for (X a : v)
    x << a << ' ';
  return x;
}
template <typename X, typename Y>
ostream &operator<<(ostream &x, const vector<pair<X, Y>> &v) {
  for (pair<X, Y> it : v)
    x << it.first << ' ' << it.second << endl;
  return x;
}
template <typename T, typename S>
ostream &operator<<(ostream &os, const map<T, S> &v) {
  for (pair<T, S> it : v)
    os << it.first << " => " << it.second << endl;
  return os;
}

double pi = 3.14159265358979323846264338327950288419716939937510582097494459230;
int modulo = 1e9 + 7;

int fpow(int a, int n) {
  int ans = 1;
  while (n) {
    if (n & 1)
      ans = (ans * a) % modulo;
    a = (a * a) % modulo;
    n = n >> 1;
  }
  return ans;
}

/*
const int M = 1e6 + 10;
int composite[M] = {0};

void sieve()
{
    FOR(i,2,M)
    {
        if (!composite[i])
        {
            for (int j = 2*i; j < M; j += i)
                composite[j] = max(composite[j], i);
        }
    }
}
*/

const int M = 2e6;
vi graph[M], sorted;
int valid[M] = {0}, n, m, visited[M] = {0};

void stringMatching(string s, string t) {
  string a = t + '#' + s;
  int n = a.size();
  int z[n];
  z[0] = 0;

  int l = 0, r = 0;
  FOR(i, 1, n) {
    if (i > r) {
      l = r = i;
      while (r < n and a[r] == a[r - l])
        ++r;
      z[i] = r - l;
      --r;
    }

    else {
      if (z[i - l] < r - i + 1)
        z[i] = z[i - l];

      else {
        l = i;
        while (r < n and a[r - l] == a[r])
          ++r;
        z[i] = r - l;
        --r;
      }
    }
  }

  FOR(i, 0, n) {
    if (z[i] == t.size())
      valid[i - t.size() - 1] = 1;
  }
}

bool isCycle(int curr, int code) {
  visited[curr] = code;
  int next = (curr + m) % n;
  if (not valid[next]) {
    sorted.pb(curr);
    return false;
  }

  if (visited[next] == code)
    return true;
  if (visited[next] == 0 and isCycle(next, code))
    return true;

  sorted.pb(curr);
  return false;
}

int dfs(int curr, int len) {
  visited[curr] = 1;
  int next = (curr + m) % n;
  if (not valid[next])
    return len;

  return dfs(next, len + 1);
}

int solve(string x, string y) {
  n = x.size();
  m = y.size();
  int req = n + m;
  string t = y, s = x;

  while (s.size() < req)
    s += x;
  stringMatching(s, t);

  int code = 1;
  FOR(i, 0, n) {
    if (valid[i] and visited[i] == 0) {
      if (isCycle(i, code))
        return -1;
      ++code;
    }
  }
  if (sorted.size() == 0)
    return 0;

  int ans = 0;
  FOR(i, 0, n) visited[i] = 0;
  FORR(i, sorted.size() - 1, -1) {
    int curr = sorted[i];
    if (valid[curr] and not visited[curr])
      ans = max(ans, dfs(curr, 1));
  }

  return ans;
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  string s, t;
  cin >> s >> t;
  cout << solve(s, t);
  return 0;
}