CSES - Shared codeLink to this code:
https://cses.fi/paste/8daf7673002020076187ba/
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
typedef long long ll;
typedef unsigned long long ull;
const int INF = 1e9;
const double EPS = 1e-9;
ll fast_ceiling(ll x, ll y) {
//(x + y - 1) / y;
if (x == 0LL) return 0LL;
return 1LL + ((x - 1LL) / y);
}
int gcd(int a, int b) {
if (a == 0) return b;
return gcd(b % a, a);
}
vector<int> sieve(int n) {
int sieve_size = n + 1;
bool sieve[sieve_size];
for (int i = 0; i < sieve_size; ++i) {
sieve[i] = true;
}
for (int i = 2; i * i <= n; ++i) {
if (sieve[i]) {
for (int j = i * i; j <= n; j += i) {
sieve[j] = false;
}
}
}
vector<int> ans;
for (int i = 2; i <= n; ++i) {
if (sieve[i]) {
ans.push_back(i);
}
}
return ans;
}
vector<ll> prime_factorization(ll n) {
ll factor = 2;
vector<ll> factors;
for (ll i = 2; i * i <= n; i++) {
if (n % i == 0) {
while(n % i == 0) {
n /= i;
}
factors.push_back(i);
}
}
if (n > 1) {
factors.push_back(n);
}
return factors;
}
const ll MOD = 998244353LL;
ll mod_multiplication(ll a, ll b) {
return (((a * b) % MOD + MOD) % MOD);
}
ll mod_power(ll x, ll n) {
ll res = 1LL;
while(n > 0LL) {
if(n % 2LL) {
res = mod_multiplication(res, x);
}
x = mod_multiplication(x, x);
n >>= 1;
}
return res;
}
ll mod_inv(ll a) {
return mod_power(a, MOD - 2LL);
}
ll nCr(ll n, ll r) {
ll res = 1LL, div = 1LL;
for(ll i = 1 ; i <= r ; i++) {
res = mod_multiplication(res, n - i + 1LL);
div = mod_multiplication(div, i);
}
res = mod_multiplication(res, mod_inv(div));
return res;
}
void setIO(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
uint32_t next_pow2(uint32_t x) { return x == 1 ? 1 : 1 << (32 - __builtin_clz(x - 1)); }
uint32_t next_pow(uint32_t x) {return x == 1 ? 0 : 32 - __builtin_clz(x - 1); }
const ll A = 911382323LL;
const ll B = 972663749LL;
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
string s;
cin >> s;
int n = s.size();
ll a_s[n];
a_s[0] = 1LL;
for (int i = 1; i < n; ++i) {
a_s[i] = (a_s[i - 1] * A) % B;
}
ll s_hash[n];
s_hash[0] = s[0];
for (int i = 1; i < n; ++i) {
s_hash[i] = ((s_hash[i - 1] * A) % B + s[i]) % B;
}
vector<ll> hashes[n - 1];
for (int len = 1; len < n; ++len) {
for (int i = 0; i < n - len + 1; ++i) {
ll sub_hash = s_hash[i + len - 1];
if (i > 0) {
sub_hash = (sub_hash + B - s_hash[i - 1] * a_s[len] % B) % B;
}
hashes[len - 1].push_back(sub_hash);
}
}
for (int i = 0; i < n - 1; ++i) {
sort(hashes[i].begin(), hashes[i].end());
}
int ans[n];
ans[n - 1] = 1;
for (int i = 0; i < n - 1; ++i) {
int distinct = 1;
for (int j = 1; j < hashes[i].size(); ++j) {
if (hashes[i][j] == hashes[i][j - 1]) continue;
distinct++;
}
ans[i] = distinct;
}
for (auto i : ans) {
cout << i << ' ';
}
return 0;
}