Problem p02962 (C++)

Buggy submission (status: Runtime Error):
// 2019.7.27 by ljz
// email 573902690@qq.com
// if you find any bug in my code
// please tell me
#include <bits/stdc++.h>
using namespace std;
#define res register int
#define LL long long
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f
#define eps 1e-10
#define RG register
#define db double
#define lb long db
#define pc(x) __builtin_popcount(x)
typedef pair<int, int> Pair;
#define mp make_pair
#define fi first
#define se second
#define pi acos(-1.0)
#define pb push_back
#define gc getchar
#define Pi 2.0 * acos(-1.0)
// inline char gc() {
//     static char buf[100000],*p1,*p2;
//     return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
// }
// inline int read() {
//     res s=0,ch=gc();
//     while(ch<'0'||ch>'9')ch=gc();
//     while(ch>='0'&&ch<='9')s=s*10+ch-'0',ch=gc();
//     return s;
// }
inline int read() {
  res s = 0, ch = gc(), w = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      w = -1;
    ch = gc();
  }
  while (ch >= '0' && ch <= '9')
    s = s * 10 + ch - '0', ch = gc();
  return s * w;
}
// inline LL Read() {
//     RG LL s=0;
//     res ch=gc();
//     while(ch<'0'||ch>'9')ch=gc();
//     while(ch>='0'&&ch<='9')s=s*10+ch-'0',ch=gc();
//     return s;
// }
// inline LL Read() {
//     RG LL s=0;
//     res ch=gc(),w=1;
//     while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=gc();}
//     while(ch>='0'&&ch<='9')s=s*10+ch-'0',ch=gc();
//     return s*w;
// }
// inline void write(RG __int128 x){
//     if(x>10)write(x/10);
//     putchar(x%10+'0');
// }
inline void swap(res &x, res &y) { x ^= y ^= x ^= y; }
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int N = 1e6 + 10;
namespace MAIN {
char S[N], T[N];
int n, m;
int F[N], G[N];
inline void MAIN() {
  scanf("%s", S + 1), scanf("%s", T + 1), n = int(strlen(S + 1)),
                                          m = int(strlen(T + 1));
  while (n < (m << 1)) {
    for (res i = 1; i <= n; i++)
      S[i + n] = S[i];
    n <<= 1;
  }
  for (res i = 1; i <= n; i++)
    S[i + n] = S[i];
  n <<= 1, F[0] = -1;
  for (res i = 1; i <= m; i++) {
    res j = i - 1;
    while (j && T[F[j] + 1] != T[i])
      j = F[j];
    F[i] = F[j] + 1;
  }
  res j = 0, ans = 0;
  for (res i = 1; i <= n; i++) {
    while (j >= 0 && S[i] != T[j + 1])
      j = F[j];
    if (++j == m)
      G[i] = G[i - m] + 1, ans = max(ans, G[i]);
  }
  printf("%d\n", ans >= n / m - 1 ? -1 : ans);
}
} // namespace MAIN
int main() {
  //    freopen("compound1.in","r",stdin);
  //    freopen("compound.out","w",stdout);
  MAIN::MAIN();
  return 0;
}

Fixed submission:
// 2019.7.27 by ljz
// email 573902690@qq.com
// if you find any bug in my code
// please tell me
#include <bits/stdc++.h>
using namespace std;
#define res register int
#define LL long long
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f
#define eps 1e-10
#define RG register
#define db double
#define lb long db
#define pc(x) __builtin_popcount(x)
typedef pair<int, int> Pair;
#define mp make_pair
#define fi first
#define se second
#define pi acos(-1.0)
#define pb push_back
#define gc getchar
#define Pi 2.0 * acos(-1.0)
// inline char gc() {
//     static char buf[100000],*p1,*p2;
//     return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
// }
// inline int read() {
//     res s=0,ch=gc();
//     while(ch<'0'||ch>'9')ch=gc();
//     while(ch>='0'&&ch<='9')s=s*10+ch-'0',ch=gc();
//     return s;
// }
inline int read() {
  res s = 0, ch = gc(), w = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      w = -1;
    ch = gc();
  }
  while (ch >= '0' && ch <= '9')
    s = s * 10 + ch - '0', ch = gc();
  return s * w;
}
// inline LL Read() {
//     RG LL s=0;
//     res ch=gc();
//     while(ch<'0'||ch>'9')ch=gc();
//     while(ch>='0'&&ch<='9')s=s*10+ch-'0',ch=gc();
//     return s;
// }
// inline LL Read() {
//     RG LL s=0;
//     res ch=gc(),w=1;
//     while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=gc();}
//     while(ch>='0'&&ch<='9')s=s*10+ch-'0',ch=gc();
//     return s*w;
// }
// inline void write(RG __int128 x){
//     if(x>10)write(x/10);
//     putchar(x%10+'0');
// }
inline void swap(res &x, res &y) { x ^= y ^= x ^= y; }
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int N = 1e7 + 10;
namespace MAIN {
char S[N], T[N];
int n, m;
int F[N], G[N];
inline void MAIN() {
  scanf("%s", S + 1), scanf("%s", T + 1), n = int(strlen(S + 1)),
                                          m = int(strlen(T + 1));
  while (n < (m << 1)) {
    for (res i = 1; i <= n; i++)
      S[i + n] = S[i];
    n <<= 1;
  }
  for (res i = 1; i <= n; i++)
    S[i + n] = S[i];
  n <<= 1, F[0] = -1;
  for (res i = 1; i <= m; i++) {
    res j = i - 1;
    while (j && T[F[j] + 1] != T[i])
      j = F[j];
    F[i] = F[j] + 1;
  }
  res j = 0, ans = 0;
  for (res i = 1; i <= n; i++) {
    while (j >= 0 && S[i] != T[j + 1])
      j = F[j];
    if (++j == m)
      G[i] = G[i - m] + 1, ans = max(ans, G[i]);
  }
  printf("%d\n", ans >= n / m - 1 ? -1 : ans);
}
} // namespace MAIN
int main() {
  //    freopen("compound1.in","r",stdin);
  //    freopen("compound.out","w",stdout);
  MAIN::MAIN();
  return 0;
}