Problem p02962 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
vector<int> table, res, v[500000], u[500000];
int ch[500000], ans, cnt, tmp, L;
string s, t, S;
map<int, int> mp;
vector<int> pre_kmp(string s) {
  vector<int> v(s.size() + 1);
  int k;
  v[0] = -1;
  for (int i = 1; i <= s.size(); i++) {
    k = v[i - 1];
    while (k >= 0) {
      if (s[k] == s[i - 1])
        break;
      else
        k = v[k];
    }
    v[i] = k + 1;
  }
  return v;
}
void match(string s, string t) {
  int head = 0, j = 0, slen = s.size(), tlen = t.size();
  while (head < slen) {
    if (t[j] == s[(head + j) % slen]) {
      if (++j != tlen)
        continue;
      res.push_back(head);
      v[head].push_back((head + tlen) % slen);
      u[(head + tlen) % slen].push_back(head);
    }
    head += j - table[j], j = max(table[j], 0);
  }
}
int rdfs(int a, int b) {
  for (auto x : u[a]) {
    if (x == b)
      return -1;
    return rdfs(x, b);
  }
  return a;
}
int dfs(int a, int cnt) {
  ch[a] = 1;
  for (auto x : v[a]) {
    return dfs(x, cnt + 1);
  }
  return cnt;
}
int main(void) {
  cin >> S >> t;
  s = S;
  while (s.size() < t.size()) {
    s += S;
  }
  // cout<<s<<" "<<t<<endl;
  table = pre_kmp(t);
  match(s, t);
  // for(auto x:res)cout<<x<<"res"<<endl;
  // cout<<rdfs(0,0)<<endl;
  for (int i = 0; i < s.size(); i++) {
    if (ch[i] != 0 || v[i].size() == 0)
      continue;
    // cout<<ch[i]<<" "<<i<<endl;
    L = rdfs(i, i);
    if (L == -1) {
      cout << -1 << endl;
      return 0;
    }
    ans = max(ans, dfs(L, 0));
    // cout<<ans<<endl;
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
vector<int> table, res, v[1000000], u[1000000];
int ch[1000000], ans, cnt, tmp, L;
string s, t, S;
map<int, int> mp;
vector<int> pre_kmp(string s) {
  vector<int> v(s.size() + 1);
  int k;
  v[0] = -1;
  for (int i = 1; i <= s.size(); i++) {
    k = v[i - 1];
    while (k >= 0) {
      if (s[k] == s[i - 1])
        break;
      else
        k = v[k];
    }
    v[i] = k + 1;
  }
  return v;
}
void match(string s, string t) {
  int head = 0, j = 0, slen = s.size(), tlen = t.size();
  while (head < slen) {
    if (t[j] == s[(head + j) % slen]) {
      if (++j != tlen)
        continue;
      res.push_back(head);
      v[head].push_back((head + tlen) % slen);
      u[(head + tlen) % slen].push_back(head);
    }
    head += j - table[j], j = max(table[j], 0);
  }
}
int rdfs(int a, int b) {
  for (auto x : u[a]) {
    if (x == b)
      return -1;
    return rdfs(x, b);
  }
  return a;
}
int dfs(int a, int cnt) {
  ch[a] = 1;
  for (auto x : v[a]) {
    return dfs(x, cnt + 1);
  }
  return cnt;
}
int main(void) {
  cin >> S >> t;
  s = S;
  while (s.size() < t.size()) {
    s += S;
  }
  // cout<<s<<" "<<t<<endl;
  table = pre_kmp(t);
  match(s, t);
  // for(auto x:res)cout<<x<<"res"<<endl;
  // cout<<rdfs(0,0)<<endl;
  for (int i = 0; i < s.size(); i++) {
    if (ch[i] != 0 || v[i].size() == 0)
      continue;
    // cout<<ch[i]<<" "<<i<<endl;
    L = rdfs(i, i);
    if (L == -1) {
      cout << -1 << endl;
      return 0;
    }
    ans = max(ans, dfs(L, 0));
    // cout<<ans<<endl;
  }
  cout << ans << endl;
}