CSES - Shared codeLink to this code:
https://cses.fi/paste/c38edc2216ec6a0165ec83/
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define sz(x) (int)(x).size()
typedef vector<int> vi;
/**
* Author: chilli
* License: CC0
* Description: z[x] computes the length of the longest common prefix of s[i:] and s, except z[0] = 0. (abacaba -> 0010301)
* Time: O(n)
* Status: stress-tested
*/
vi Z(const string& S) {
vi z(sz(S));
int l = 0, r = 0;
rep(i,1,sz(S)) {
z[i] = min(max(r - i, 0), z[i - l]);
while (i + z[i] < sz(S) && S[i + z[i]] == S[z[i]])
z[i]++, l = i, r = i + z[i];
}
return z;
}
int main() {
cin.tie(0)->sync_with_stdio(0);
string s;
cin >> s;
vector<int> z(Z(s));
for(int sz = 1; sz <= sz(s); sz++) {
int curr = sz;
if(curr < sz(s)) {
curr += z[curr];
}
if(curr == sz(s)) cout << sz << " ";
}
return 0;
}