Problem p02962 (C++)

Buggy submission (status: Runtime Error):
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;

template <typename T> vector<int> z_algorithm(const int len, const T &in) {
  vector<int> res(len, 0);
  int c = 0;
  for (int i = 1; i < len; ++i) {
    if (i + res[i - c] < c + res[c]) {
      res[i] = res[i - c];
    } else {
      int j = max(0, c + res[c] - i);
      while (i + j < len && in[j] == in[i + j])
        j++;
      res[i] = j;
      c = i;
    }
  }
  res[0] = len;
  return res;
}

template <typename T> vector<int> z_algorithm(const T &in) {
  return z_algorithm((int)in.size(), in);
}

template <typename T>
vector<int> z_algorithm(const T &in_first, const T &in_second) {
  T combined = T();
  combined.append(in_first);
  combined.append(in_second);
  int n = (int)in_first.size(), m = (int)in_second.size();
  vector<int> v = z_algorithm(n + m, combined);
  vector<int> res(m);
  for (int i = 0; i < m; i++) {
    res[i] = min(n, v[i + n]);
  }
  return res;
}

class UnionFind {
private:
  int siz;
  vector<int> a;

public:
  UnionFind(int x) : siz(x), a(x, -1) {}

  int root(int x) { return a[x] < 0 ? x : a[x] = root(a[x]); }

  bool unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    siz--;
    if (a[x] > a[y])
      swap(x, y);
    a[x] += a[y];
    a[y] = x;
    return true;
  }

  bool same(int x, int y) { return root(x) == root(y); }

  int size(int x) { return -a[root(x)]; }

  int conneted_component() { return siz; }
};

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  string s, t;
  cin >> s >> t;
  int n = (int)s.size(), m = (int)t.size();
  // s.append(s);
  // s.append(s);
  // s.append(s);
  string s2;
  while (2 * (int)t.size() >= (int)s2.size())
    s2.append(s);
  assert((int)t.size() * 2 <= (int)s.size());
  vector<int> v = z_algorithm(t, s);
  UnionFind uf(n);
  for (int i = 0; i < n; i++) {
    if (v[i] == m && v[i + m] == m) {
      if (!uf.unite(i, (i + m) % n)) {
        cout << -1 << endl;
        return 0;
      }
    }
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (v[i] == m)
      ans = max(ans, uf.size(i));
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;

template <typename T> vector<int> z_algorithm(const int len, const T &in) {
  vector<int> res(len, 0);
  int c = 0;
  for (int i = 1; i < len; ++i) {
    if (i + res[i - c] < c + res[c]) {
      res[i] = res[i - c];
    } else {
      int j = max(0, c + res[c] - i);
      while (i + j < len && in[j] == in[i + j])
        j++;
      res[i] = j;
      c = i;
    }
  }
  res[0] = len;
  return res;
}

template <typename T> vector<int> z_algorithm(const T &in) {
  return z_algorithm((int)in.size(), in);
}

template <typename T>
vector<int> z_algorithm(const T &in_first, const T &in_second) {
  T combined = T();
  combined.append(in_first);
  combined.append(in_second);
  int n = (int)in_first.size(), m = (int)in_second.size();
  vector<int> v = z_algorithm(n + m, combined);
  vector<int> res(m);
  for (int i = 0; i < m; i++) {
    res[i] = min(n, v[i + n]);
  }
  return res;
}

class UnionFind {
private:
  int siz;
  vector<int> a;

public:
  UnionFind(int x) : siz(x), a(x, -1) {}

  int root(int x) { return a[x] < 0 ? x : a[x] = root(a[x]); }

  bool unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    siz--;
    if (a[x] > a[y])
      swap(x, y);
    a[x] += a[y];
    a[y] = x;
    return true;
  }

  bool same(int x, int y) { return root(x) == root(y); }

  int size(int x) { return -a[root(x)]; }

  int conneted_component() { return siz; }
};

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  string s, t;
  cin >> s >> t;
  int n = (int)s.size(), m = (int)t.size();
  s += s;
  s += s;
  while ((int)t.size() * 2 > (int)s.size())
    s += s;
  vector<int> v = z_algorithm(t, s);
  UnionFind uf(n);
  for (int i = 0; i < n; i++) {
    if (v[i] == m && v[i + m] == m) {
      if (!uf.unite(i, (i + m) % n)) {
        cout << -1 << endl;
        return 0;
      }
    }
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (v[i] == m)
      ans = max(ans, uf.size(i));
  }
  cout << ans << endl;
  return 0;
}