Link to this code:
https://cses.fi/paste/84b45d5652a6f9c5e19fe9/#ifdef ONPC
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
#define pii pair<int, int>
using namespace std;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
#define vi vector<int>
#define sz(a) a.size()
#define rep(a, b, c) for (int a = b; a < c; ++a)
#pragma once
vector<int> ans;
array<vi, 2> manacher(const string &s) // 0 is manacher even
{
int n = sz(s);
array<vi, 2> p = {vi(n + 1), vi(n)};
rep(z, 0, 2) for (int i = 0, l = 0, r = 0; i < n; i++)
{
int t = r - i + !z;
// cout << t << ' ' << r << ' ' << ' ' << i << endl;
if (i < r)
p[z][i] = min(t, p[z][l + t]);
int L = i - p[z][i], R = i + p[z][i] - !z;
// cout << L << ' ' << R << ' ' << endl;
while (L >= 1 && R + 1 < n && s[L - 1] == s[R + 1])
{
p[z][i]++, L--, R++;
if (z == 0)
{
int len = p[z][i];
if (len == 0)
{
continue;
}
int end = i + len - 1;
int dist = len * 2;
if (0 <= end and end < n)
ans[end] = max(ans[end], dist);
}
else
{
int len = p[z][i];
if (len == 0)
continue;
int end = i + len;
int dist = (len * 2) + 1;
if (0 <= end and end < n)
ans[end] = max(ans[end], dist);
}
}
// cout << endl;
if (R > r)
l = L, r = R;
}
return p;
}
void solve()
{
string s;
cin >> s;
int n = s.length();
ans.assign(n, 0);
auto man = manacher(s);
for (auto i : ans)
{
cout << (i == 0 ? 1 : i) << ' ';
}
cout << endl;
return;
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int t = 1;
#ifdef ONPC
freopen("input.txt", "r", stdin);
#endif
// cin >> t;
for (int i = 0; i < t; ++i)
{
solve();
}
#ifdef ONPC
cerr << endl
<< "finished in " << clock() * 1.0 / CLOCKS_PER_SEC << " sec" << endl;
#endif
return 0;
}