#include using namespace std; using i64 = int64_t; using vi = vector; using vvi = vector; mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); i64 modpow(i64 a, i64 n, i64 mod) { if (n == 0) return 1; if (n % 2 == 0) { i64 t = modpow(a, n / 2, mod); return t * t % mod; } return a * modpow(a, n - 1, mod) % mod; } i64 modinv(i64 a, i64 mod) { return modpow(a, mod - 2, mod); } bool is_prime(i64 n, int k = 50) { if (n == 2) return true; if (n % 2 == 0 || n < 2) return false; i64 d = n - 1; while (d % 2 == 0) { d /= 2; } for (int i = 0; i < k; i++) { i64 a = rnd() % (n - 2) + 1; i64 t = d; i64 y = modpow(a, t, n); while (t != n - 1 && y != 1 && y != n - 1) { y = modpow(y, 2, n); t *= 2; } if (y != n - 1 && t % 2 == 0) { return false; } } return true; } i64 gen_prime() { while (1) { i64 d = rnd() & ((i64(1) << 30) - 1); d |= 1; if (is_prime(d)) { return d; } } } struct RollingHash { vi hash; vi pows; i64 p, m; RollingHash(string s, i64 p = gen_prime(), i64 m = gen_prime()) : hash(s.size() + 1), pows(s.size() + 1), p(p), m(m) { hash[0] = 1; pows[0] = 1; for (int i = 0; i < s.size(); i++) { hash[i + 1] = (hash[i] * p % m + s[i]) % m; pows[i + 1] = pows[i] * p % m; } } // [l, r) i64 encode(int l, int r) { return ((hash[r] - hash[l] * pows[r - l] % m) % m + m) % m; } // [l, r)のうち[ml, mr)をスキップした文字列のencode i64 skipencode(int l, int r, int ml, int mr) { assert(l <= ml && ml <= mr && mr <= r); return (encode(l, ml) * pows[r - mr] % m + encode(mr, r)) % m; } bool eq(int l0, int r0, int l1, int r1) { return encode(l0, r0) == encode(l1, r1); } }; int main() { string S; cin >> S; string s = "$"; for (char c : S) { s += c; s += '$'; } int n = s.size(); string t(s); reverse(t.begin(), t.end()); i64 p = gen_prime(), p2 = gen_prime(); RollingHash rh(s, p, p2), rh2(t, p, p2); int nax = 1; for (int i = 1; i < n - 1; i++) { cerr << "i = " << i << endl; int l = 0, r = min(i, n - 1 - i) + 1; auto ok = [&](int m) { return rh.encode(i + 1, i + 1 + m) == rh2.encode(n - i, n - i + m); }; while (l < r - 1) { int m = (l + r) / 2; if (ok(m)) { l = m; } else { r = m; } } int K = l; cerr << s << endl; cerr << "when center is " << i << ", longest common LR = " << l << endl; if (n - 1 - i == l && i == l) { nax = max(nax, l - 2); continue; } if (n - 1 - i == l) { nax = max(nax, l); } else { r = min(i, n - 1 - i) + 1; auto ok2 = [&](int m) { if (i + 1 + m + 2 >= n) return false; return rh.skipencode(i + 1, i + 1 + m + 2, i + 1 + K, i + 1 + K + 2) == rh2.encode(n - i, n - i + m); }; while (l < r - 1) { int m = (l + r) / 2; if (ok2(m)) { l = m; } else { r = m; } } cerr << "when erase right, the longest commond LR = " << l << endl; nax = max(nax, l); } l = K; if (i == l) { nax = max(nax, l - 1); } else { r = min(i, n - 1 - i) + 1; auto ok3 = [&](int m) { if (n - i + m + 2 >= n) return false; return rh.encode(i + 1, i + 1 + m) == rh2.skipencode(n - i, n - i + m + 2, n - i + K, n - i + K + 2); }; while (l < r - 1) { int m = (l + r) / 2; if (ok3(m)) { l = m; } else { r = m; } } cerr << "when erase left, the longest commond LR = " << l << endl; nax = max(nax, l); } } cout << nax << endl; }