Problem p02962 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>     // minmax, sort, swap
#include <climits>       // INT_MIN, LLONG_MIN
#include <cmath>         // long, trig, pow
#include <cstdio>        // printf, scanf
#include <deque>         // deque
#include <functional>    // std::function<void(int)>
#include <iomanip>       // cout<<setprecision(n)
#include <iostream>      // cin, cout, cerr, clog
#include <map>           // key-value pairs sorted by keys
#include <numeric>       // iota, accumulate, inner_product
#include <queue>         // queue, priority_queue
#include <set>           // set
#include <string>        // string, stoi, to_string
#include <unordered_map> // hashed by keys
#include <unordered_set> // hashed by keys
#include <vector>        // vector

#define rep(i, n) for (int i = 0; i < n; i++)
#define ENDL "\n"
#define print(i) std::cout << (i) << "\n"

#define int long long // at least int64 > 9*10^18
#define all(v) v.begin(), v.end()

#define ceil(a, b) (a % b ? a / b + 1 : a / b)

#define dump1(v)                                                               \
  for (auto i : v)                                                             \
    std::cout << i << ' ';                                                     \
  std::cout << "\n";

struct FastIO {
  FastIO() {
    std::cin.tie(0);
    std::ios_base::sync_with_stdio(0);
  }
} fastio;

struct ZAlgorithm {
  std::vector<int> z;
  ZAlgorithm(std::string s) : z(s.size(), 0) {
    int l, r;
    // [l,r)
    l = r = 0;
    for (int i = 1; i < s.size(); i++) {
      if (i >= r) {
        l = r = i;
        while (r < s.size() && s[r] == s[r - l])
          r++;
        z[i] = r - l;
        continue;
      }
      if (z[i - l] < r - i) {
        z[i] = z[i - l];
        continue;
      }
      l = i;
      while (r < s.size() && s[r] == s[r - l])
        r++;
      z[i] = r - l;
      continue;
    }
    z[0] = s.size();
  }
};

int dfs(int i, int c, std::vector<int> &to, std::vector<int> &went) {
  if (to[c] == i)
    return -1;
  if (to[c] == -1)
    return 0;
  if (went[c] > 0)
    return went[c];
  int x = dfs(i, to[c], to, went);
  if (x == -1)
    return -1;
  return went[c] = x + 1;
}

signed main() {
  std::string s;
  std::string t;
  std::cin >> s >> t;

  std::string a = "";

  int t_sz = t.size();
  int s_sz = s.size();
  int f = ceil(t_sz, s_sz) + 1;
  rep(i, f) a = a + s;

  std::string x = t + '$' + a;
  ZAlgorithm z(x);

  std::vector<int> zz(s_sz);
  rep(i, s_sz) zz[i] = z.z[t_sz + 1 + i];

  std::vector<int> to(s_sz, -1);
  rep(i, s_sz) if (zz[i] == t_sz) to[i] = (i + t_sz) % s_sz;

  std::vector<int> went(s_sz, 0);
  rep(i, s_sz) {
    if (zz[i] != t_sz)
      continue;

    if (dfs(i, i, to, went) == -1) {
      print(-1);
      return 0;
    }
  }
  int max = 0;
  for (auto i : went)
    max = std::max(max, i);
  print(max);
  return 0;
}


Fixed submission:
#include <algorithm>     // minmax, sort, swap
#include <climits>       // INT_MIN, LLONG_MIN
#include <cmath>         // long, trig, pow
#include <cstdio>        // printf, scanf
#include <deque>         // deque
#include <functional>    // std::function<void(int)>
#include <iomanip>       // cout<<setprecision(n)
#include <iostream>      // cin, cout, cerr, clog
#include <map>           // key-value pairs sorted by keys
#include <numeric>       // iota, accumulate, inner_product
#include <queue>         // queue, priority_queue
#include <set>           // set
#include <string>        // string, stoi, to_string
#include <unordered_map> // hashed by keys
#include <unordered_set> // hashed by keys
#include <vector>        // vector

#define rep(i, n) for (int i = 0; i < n; i++)
#define ENDL "\n"
#define print(i) std::cout << (i) << "\n"

#define int long long // at least int64 > 9*10^18
#define all(v) v.begin(), v.end()

#define ceil(a, b) (a % b ? a / b + 1 : a / b)

#define dump1(v)                                                               \
  for (auto i : v)                                                             \
    std::cout << i << ' ';                                                     \
  std::cout << "\n";

struct FastIO {
  FastIO() {
    std::cin.tie(0);
    std::ios_base::sync_with_stdio(0);
  }
} fastio;

struct ZAlgorithm {
  std::vector<int> z;
  ZAlgorithm(std::string s) : z(s.size(), 0) {
    int l, r;
    // [l,r)
    l = r = 0;
    for (int i = 1; i < s.size(); i++) {
      if (i >= r) {
        l = r = i;
        while (r < s.size() && s[r] == s[r - l])
          r++;
        z[i] = r - l;
        continue;
      }
      if (z[i - l] < r - i) {
        z[i] = z[i - l];
        continue;
      }
      l = i;
      while (r < s.size() && s[r] == s[r - l])
        r++;
      z[i] = r - l;
      continue;
    }
    z[0] = s.size();
  }
};

int dfs(int i, int c, std::vector<int> &to, std::vector<int> &went) {
  if (to[c] == i)
    return -1;
  if (to[c] == -1)
    return 0;
  if (went[c] > 0)
    return went[c];
  int x = dfs(i, to[c], to, went);
  if (x == -1)
    return -1;
  return went[c] = x + 1;
}

signed main() {
  std::string s;
  std::string t;
  std::cin >> s >> t;

  std::string a = "";

  int t_sz = t.size();
  int s_sz = s.size();
  int f = ceil(t_sz, s_sz) + 1;
  rep(i, f) a += s;

  std::string x = t + '$' + a;
  ZAlgorithm z(x);

  std::vector<int> zz(s_sz);
  rep(i, s_sz) zz[i] = z.z[t_sz + 1 + i];

  std::vector<int> to(s_sz, -1);
  rep(i, s_sz) if (zz[i] == t_sz) to[i] = (i + t_sz) % s_sz;

  std::vector<int> went(s_sz, 0);
  rep(i, s_sz) {
    if (zz[i] != t_sz)
      continue;

    if (dfs(i, i, to, went) == -1) {
      print(-1);
      return 0;
    }
  }
  int max = 0;
  for (auto i : went)
    max = std::max(max, i);
  print(max);
  return 0;
}