Problem p02962 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

class Solution {
public:
  int solve(string &s, string &t) {
    string S = s;
    while (S.length() < t.length()) {
      S += s;
    }
    int n = S.length();

    S += S;

    int m = t.length();
    vector<int> next(m + 1, -1);
    for (int i = 0, j = -1; i < n; ++i) {
      while (j != -1 && t[j] != t[i]) {
        j = next[j];
      }
      next[i + 1] = ++j;
    }

    vector<int> pos;
    for (int i = 0, j = 0; i < S.length(); ++i) {
      if (S[i] == t[j]) {
        if (++j == m) {
          int beg = i - m + 1;
          pos.push_back(beg % n);
          j = next[j];
        }
      } else {
        while (j != -1 && S[i] != t[j]) {
          j = next[j];
        }
        if (j == -1) {
          j = 0;
        } else {
          --i;
        }
      }
    }

    vector<vector<int>> graph(n);
    vector<int> degrees(n, 0);

    vector<bool> ok(n, false);
    for (auto x : pos) {
      ok[x] = true;
    }
    for (int i = 0; i < n; ++i) {
      if (ok[i]) {
        int j = (i + m) % n;
        if (ok[j]) {
          graph[i].push_back(j);
          degrees[j] += 1;
        }
      }
    }

    vector<int> ord;
    for (int i = 0; i < n; ++i) {
      if (degrees[i] == 0) {
        ord.push_back(i);
      }
    }

    for (int i = 0; i < ord.size(); ++i) {
      int u = ord[i];
      for (auto v : graph[u]) {
        if (--degrees[v] == 0) {
          ord.push_back(v);
        }
      }
    }

    for (int i = 0; i < n; ++i) {
      if (degrees[i] != 0) {
        return -1;
      }
    }

    reverse(ord.begin(), ord.end());

    vector<int> dp(n, 0);

    for (auto u : ord) {
      if (!ok[u]) {
        continue;
      }
      int ans = 0;
      for (auto v : graph[u]) {
        ans = max(ans, dp[v]);
      }
      ans += 1;
      dp[u] = ans;
    }

    int res = *max_element(dp.begin(), dp.end());
    return res;
  }
};

int main(int argc, char **argv) {
  ios::sync_with_stdio(false);
  cin.tie(0);

  string s;
  string t;
  cin >> s >> t;

  Solution sol;
  cout << sol.solve(s, t) << "\n";

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

class Solution {
public:
  int solve(string &s, string &t) {
    string S = s;
    while (S.length() < t.length()) {
      S += s;
    }
    int n = S.length();

    S += S;

    int m = t.length();
    vector<int> next(m + 1, -1);
    for (int i = 0, j = -1; i < m; ++i) {
      while (j != -1 && t[j] != t[i]) {
        j = next[j];
      }
      next[i + 1] = ++j;
    }

    vector<int> pos;
    for (int i = 0, j = 0; i < S.length(); ++i) {
      if (S[i] == t[j]) {
        if (++j == m) {
          int beg = i - m + 1;
          pos.push_back(beg % n);
          j = next[j];
        }
      } else {
        while (j != -1 && S[i] != t[j]) {
          j = next[j];
        }
        if (j == -1) {
          j = 0;
        } else {
          --i;
        }
      }
    }

    vector<vector<int>> graph(n);
    vector<int> degrees(n, 0);

    vector<bool> ok(n, false);
    for (auto x : pos) {
      ok[x] = true;
    }
    for (int i = 0; i < n; ++i) {
      if (ok[i]) {
        int j = (i + m) % n;
        if (ok[j]) {
          graph[i].push_back(j);
          degrees[j] += 1;
        }
      }
    }

    vector<int> ord;
    for (int i = 0; i < n; ++i) {
      if (degrees[i] == 0) {
        ord.push_back(i);
      }
    }

    for (int i = 0; i < ord.size(); ++i) {
      int u = ord[i];
      for (auto v : graph[u]) {
        if (--degrees[v] == 0) {
          ord.push_back(v);
        }
      }
    }

    for (int i = 0; i < n; ++i) {
      if (degrees[i] != 0) {
        return -1;
      }
    }

    reverse(ord.begin(), ord.end());

    vector<int> dp(n, 0);

    for (auto u : ord) {
      if (!ok[u]) {
        continue;
      }
      int ans = 0;
      for (auto v : graph[u]) {
        ans = max(ans, dp[v]);
      }
      ans += 1;
      dp[u] = ans;
    }

    int res = *max_element(dp.begin(), dp.end());
    return res;
  }
};

int main(int argc, char **argv) {
  ios::sync_with_stdio(false);
  cin.tie(0);

  string s;
  string t;
  cin >> s >> t;

  Solution sol;
  cout << sol.solve(s, t) << "\n";

  return 0;
}