#include #define rep(i,n) for(int i = 0; i < (n); i++) using namespace std; typedef long long ll; struct RollingHash { static constexpr int M = 2; static constexpr long long MODS[M] = {999999937, 1000000007}; static constexpr long long BASE = 9973; vector powb[M], hash[M]; int n; RollingHash() {} RollingHash(const string& str) { init(str); } void init(const string& str) { n = str.size(); for (int k = 0; k < M; k++) { powb[k].resize(n + 1, 1); hash[k].resize(n + 1, 0); for (int i = 0; i < n; i++) { hash[k][i + 1] = (hash[k][i] * BASE + str[i]) % MODS[k]; powb[k][i + 1] = powb[k][i] * BASE % MODS[k]; } } } // get hash str[l,r) long long get(int l, int r, int k) { long long res = hash[k][r] - hash[k][l] * powb[k][r - l] % MODS[k]; if (res < 0) res += MODS[k]; return res; } }; bool match(RollingHash& rh1, int l1, int r1, RollingHash& rh2, int l2, int r2) { bool res = true; for (int k = 0; k < RollingHash::M; k++) { res &= rh1.get(l1, r1, k) == rh2.get(l2, r2, k); } return res; } int main(){ cin.tie(0); ios::sync_with_stdio(0); string S; cin >> S; int N = S.size(); string T = S; reverse(T.begin(), T.end()); if(S == T) { cout << N - 1 << endl; return 0; } RollingHash HS(S), HT(T); int ans = 0; rep(i,N) { int L = 0, R = min(i, N - (i + 1)) + 1; while(R - L > 1) { int x = (R + L) / 2; (match(HS, i + 1, i + 1 + x, HT, N - i, N - i + x) ? L : R) = x; } if(L == min(i, N - (i + 1))) { ans = max(ans, L + 1 + L); continue; } int SL = L, SR = R; { L = SL, R = SR; int pos = L; R = min(i, N - (i + 2)) + 1; while(R - L > 1) { int x = (R + L) / 2; (match(HS, i + 2 + pos, i + 2 + x, HT, N - i + pos, N - i + x) ? L : R) = x; } ans = max(ans, L + 1 + L); } { L = SL, R = SR; int pos = L; R = min(i - 1, N - (i + 1)) + 1; while(R - L > 1) { int x = (R + L) / 2; (match(HS, i + 1 + pos, i + 1 + x, HT, N - i + pos + 1, N - i + x + 1) ? L : R) = x; } ans = max(ans, L + 1 + L); } } rep(i,N-1) if(S[i] == S[i + 1]) { int L = 0, R = min(i, N - (i + 2)) + 1; while(R - L > 1) { int x = (R + L) / 2; (match(HS, i + 2, i + 2 + x, HT, N - i, N - i + x) ? L : R) = x; } if(L == min(i, N - (i + 2))) { ans = max(ans, L + 2 + L); continue; } int SL = L, SR = R; { L = SL, R = SR; int pos = L; R = min(i, N - (i + 3)) + 1; while(R - L > 1) { int x = (R + L) / 2; (match(HS, i + 3 + pos, i + 3 + x, HT, N - i + pos, N - i + x) ? L : R) = x; } ans = max(ans, L + 2 + L); } { L = SL, R = SR; int pos = L; R = min(i - 1, N - (i + 2)) + 1; while(R - L > 1) { int x = (R + L) / 2; (match(HS, i + 2 + pos, i + 2 + x, HT, N - i + pos + 1, N - i + x + 1) ? L : R) = x; } ans = max(ans, L + 2 + L); } } cout << ans << endl; }