Language: C++

#include <cassert>
#include <cstdio>
#include <cmath>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <vector>
#include <set>
#include <unordered_map>
#include <queue>
#include <numeric>
#include <algorithm>

using namespace std;
using lint = long long;
constexpr int MOD = 1000000007, INF = 1010101010;
constexpr lint LINF = 1LL << 60;

template <class T>
ostream &operator<<(ostream &os, const vector<T> &vec) {
	for (const auto &e : vec) os << e << (&e == &vec.back() ? "\n" : " ");
	return os;
}

#ifdef _DEBUG
template <class T>
void dump(const char* str, T &&h) { cerr << str << " = " << h << "\n"; };
template <class Head, class... Tail>
void dump(const char* str, Head &&h, Tail &&... t) {
	while (*str != ',') cerr << *str++; cerr << " = " << h << "\n";
	dump(str + (*(str + 1) == ' ' ? 2 : 1), t...);
}
#define DMP(...) dump(#__VA_ARGS__, __VA_ARGS__)
#else 
#define DMP(...) ((void)0)
#endif

template<class T>
inline bool chmax(T &a, T b) { return a < b && (a = b, true); }

int ranks[32][200000];
void karp_miller_rosenberg_pre(char *text) {
  int n = strlen(text), m = 0;
  pair<pair<int,int>,int> NUM[n];
  copy(text, text+n, ranks[m]);
  for (int k = 0; k <= n; k ? k*=2 : ++k) { // power of 2
    for (int i = 0; i < n; ++i) SETNUM(ranks[m], i, k);
    if (k > 0) ++m; RENUMBER(ranks[m]);
  }
}
int repeated_factor(char *text, int r, int k) {
  int n = strlen(text), t, s;
  pair<pair<int,int>,int> NUM[n];
  int rank[n], count[n];
  for (s = 1, t = 0; s * 2 < r; s *= 2, ++t);
  for (int i = 0; i < n; ++i) SETNUM(ranks[t], i, r-s);
  RENUMBER(rank);
  memset(count, 0, sizeof(count));
  for (int i = 0; i < n; ++i)
    if (++count[ rank[i] ] >= k) return i;
  return -1;
}
string longest_repeated_factor(char *text, int k) {
  karp_miller_rosenberg_pre(text);
  int low = 1, high = strlen(text);
  while (low+1 < high) {
    int mid = (low + high) / 2;
    if (repeated_factor(text, mid, k) < 0) high = mid;
    else                                    low = mid;
  }
  int t = repeated_factor(text, low, k);
  return t >= 0 ? string(text+t, text+t+low+1) : "";
}

int main() {

	cin.tie(nullptr);
	ios::sync_with_stdio(false);

	int N;
	cin >> N;
	string s;
	cin >> s;

	auto check = [&](int len) {

		unordered_map<string, int> mp;
		for (int j = 0; j <= N - len; j++) {
			string tmp = s.substr(j, len);
			auto itr = mp.find(tmp);
			if (itr == mp.end()) mp.emplace(tmp, j);
			else if (j - itr->second >= len) return true;
		}
		
		return false;
	};

	vector<int> len(N/2 + 2);
	iota(len.begin(), len.end(), 0);
	cout << *partition_point(len.begin(), len.end(), check) - 1 << endl;

	return 0;
}


Compiler output:
a.cc: In function 'void karp_miller_rosenberg_pre(char*)':
a.cc:43:11: error: 'strlen' was not declared in this scope
   43 |   int n = strlen(text), m = 0;
      |           ^~~~~~
a.cc:13:1: note: 'strlen' is defined in header '<cstring>'; this is probably fixable by adding '#include <cstring>'
   12 | #include <algorithm>
  +++ |+#include <cstring>
   13 | 
a.cc:45:28: error: 'm' was not declared in this scope
   45 |   copy(text, text+n, ranks[m]);
      |                            ^
a.cc:47:33: error: 'SETNUM' was not declared in this scope
   47 |     for (int i = 0; i < n; ++i) SETNUM(ranks[m], i, k);
      |                                 ^~~~~~
a.cc:48:21: error: 'RENUMBER' was not declared in this scope
   48 |     if (k > 0) ++m; RENUMBER(ranks[m]);
      |                     ^~~~~~~~
a.cc: In function 'int repeated_factor(char*, int, int)':
a.cc:52:11: error: 'strlen' was not declared in this scope
   52 |   int n = strlen(text), t, s;
      |           ^~~~~~
a.cc:52:11: note: 'strlen' is defined in header '<cstring>'; this is probably fixable by adding '#include <cstring>'
a.cc:55:8: error: 's' was not declared in this scope
   55 |   for (s = 1, t = 0; s * 2 < r; s *= 2, ++t);
      |        ^
a.cc:55:15: error: 't' was not declared in this scope
   55 |   for (s = 1, t = 0; s * 2 < r; s *= 2, ++t);
      |               ^
a.cc:56:44: error: 't' was not declared in this scope
   56 |   for (int i = 0; i < n; ++i) SETNUM(ranks[t], i, r-s);
      |                                            ^
a.cc:56:53: error: 's' was not declared in this scope
   56 |   for (int i = 0; i < n; ++i) SETNUM(ranks[t], i, r-s);
      |                                                     ^
a.cc:56:31: error: 'SETNUM' was not declared in this scope
   56 |   for (int i = 0; i < n; ++i) SETNUM(ranks[t], i, r-s);
      |                               ^~~~~~
a.cc:57:3: error: 'RENUMBER' was not declared in this scope
   57 |   RENUMBER(rank);
      |   ^~~~~~~~
a.cc:58:3: error: 'memset' was not declared in this scope
   58 |   memset(count, 0, sizeof(count));
      |   ^~~~~~
a.cc:58:3: note: 'memset' is defined in header '<cstring>'; this is probably fixable by adding '#include <cstring>'
a.cc: In function 'std::string longest_repeated_factor(char*, int)':
a.cc:65:23: error: 'strlen' was not declared in this scope
   65 |   int low = 1, high = strlen(text);
      |                       ^~~~~~
a.cc:65:23: note: 'strlen' is defined in header '<cstring>'; this is probably fixable by adding '#include <cstring>'