#ifdef __LOCAL #include // プリコンパイル済みヘッダ ~/local/include/mytemplate.hpp.gch #else #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #endif using namespace std; // #include // using bint = boost::multiprecision::cpp_int; using int128 = __int128_t; using ll = long long; using vl = vector; using vvl = vector; using vb = vector; using vvb = vector; using vd = vector; using vvd = vector; using vc = vector; using vvc = vector; using pll = pair; #define REP1(i, n) REP3(i, 0, n, 1) #define REP2(i, s, n) REP3(i, s, n, 1) #define REP3(i, s, n, d) for(ll i = (ll)(s); i < (ll)(n); i += (d)) #define REP_OVERLOAD(e1, e2, e3, e4, NAME,...) NAME #define rep(...) REP_OVERLOAD(__VA_ARGS__, REP3, REP2, REP1)(__VA_ARGS__) #define DEP1(i, n) DEP3(i, n, -1, 1) #define DEP2(i, n, s) DEP3(i, n, s, 1) #define DEP3(i, n, s, d) for(ll i = (ll)(n); (ll)(s) < i; i -= (d)) #define DEP_OVERLOAD(e1, e2, e3, e4, NAME,...) NAME #define dep(...) DEP_OVERLOAD(__VA_ARGS__, DEP3, DEP2, DEP1)(__VA_ARGS__) #define fore(e, a) for (auto&& e: (a)) #define len(a) (ll)(a).size() #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() #define pb push_back #define eb emplace_back #define fi first #define se second #define th third /********* C O N S T A N T **********/ constexpr ll INF = LONG_LONG_MAX / 2 - 10000LL; // 4,611,686,018,427,377,903 ~= 4.6e+18, 19桁 const double PI = acos(-1); /********* P A I R **********/ template inline pair& operator+=(pair& a, const pair& b) {a.fi += b.fi; a.se += b.se; return a;} template inline pair& operator-=(pair& a, const pair& b) {a.fi -= b.fi; a.se -= b.se; return a;} template inline pair& operator*=(pair& a, const pair& b) {a.fi *= b.fi; a.se *= b.se; return a;} template inline pair& operator/=(pair& a, const pair& b) {a.fi /= b.fi; a.se /= b.se; return a;} template inline pair& operator%=(pair& a, const pair& b) {a.fi %= b.fi; a.se %= b.se; return a;} template inline pair& operator+=(pair& a, const V& b) {a.fi += b; a.se += b; return a;} template inline pair& operator-=(pair& a, const V& b) {a.fi -= b; a.se -= b; return a;} template inline pair& operator*=(pair& a, const V& b) {a.fi *= b; a.se *= b; return a;} template inline pair& operator/=(pair& a, const V& b) {a.fi /= b; a.se /= b; return a;} template inline pair& operator%=(pair& a, const V& b) {a.fi %= b; a.se %= b; return a;} template inline pair operator+(pair a, const V& b) {a += b; return a;} template inline pair operator-(pair a, const V& b) {a -= b; return a;} template inline pair operator*(pair a, const V& b) {a *= b; return a;} template inline pair operator/(pair a, const V& b) {a /= b; return a;} template inline pair operator%(pair a, const V& b) {a %= b; return a;} template inline pair operator+(pair a) {return a;} template inline pair operator-(pair a) {return a * (-1);} template istream& operator >>(istream& stream, pair& a) {stream >> a.fi >> a.se; return stream;} template ostream& operator <<(ostream &stream, const pair& a) { stream << a.fi << " " << a.se; return stream; } /********* V E C T O R **********/ template inline vector& operator+=(vector& a, const vector& b) {assert(a.size() == b.size()); rep(i, 0, a.size()) a[i] += b[i]; return a;} template inline vector& operator-=(vector& a, const vector& b) {assert(a.size() == b.size()); rep(i, 0, a.size()) a[i] -= b[i]; return a;} template inline vector& operator*=(vector& a, const vector& b) {assert(a.size() == b.size()); rep(i, 0, a.size()) a[i] *= b[i]; return a;} template inline vector& operator/=(vector& a, const vector& b) {assert(a.size() == b.size()); rep(i, 0, a.size()) a[i] /= b[i]; return a;} template inline vector& operator%=(vector& a, const vector& b) {assert(a.size() == b.size()); rep(i, 0, a.size()) a[i] %= b[i]; return a;} template inline vector& operator+=(vector& a, const U& b) {fore(e, a) e += b; return a;} template inline vector& operator-=(vector& a, const U& b) {fore(e, a) e -= b; return a;} template inline vector& operator*=(vector& a, const U& b) {fore(e, a) e *= b; return a;} template inline vector& operator/=(vector& a, const U& b) {fore(e, a) e /= b; return a;} template inline vector& operator%=(vector& a, const U& b) {fore(e, a) e %= b; return a;} template inline vector operator+(vector a, const U& b) {a += b; return a;} template inline vector operator-(vector a, const U& b) {a -= b; return a;} template inline vector operator*(vector a, const U& b) {a *= b; return a;} template inline vector operator/(vector a, const U& b) {a /= b; return a;} template inline vector operator%(vector a, const U& b) {a %= b; return a;} template inline vector operator+(vector a) {return a;} template inline vector operator-(vector a) {return a * (-1);} template istream& operator >>(istream& stream, vector& a) {fore(e, a) stream >> e; return stream;} template ostream& operator <<(ostream& stream, const vector& v) {if(v.size()){stream << v[0]; rep(i, 1, v.size()) cout << " " << v[i];} return stream;} template ostream& operator <<(ostream& stream, const vector>& vv) {if(vv.size()){stream << vv[0]; rep(i, 1, vv.size()) cout << '\n' << vv[i];} return stream;} template inline T Sum(const vector& v) {return reduce(all(v));} // v.size() == 0 のとき T() を返す template inline T Max(const vector& v) {assert(v.size()); return *max_element(all(v));} template inline T Min(const vector& v) {assert(v.size()); return *min_element(all(v));} template inline ll Argmax(const vector& v) {assert(v.size()); return max_element(all(v)) - v.begin();} template inline ll Argmin(const vector& v) {assert(v.size()); return min_element(all(v)) - v.begin();} template inline bool Contains(const vector& v, const U& a) {return find(all(v), a) != v.end();} template vector make_vector(int n, U v) { return vector(n, v); } template auto make_vector(int n, Args... args) {auto val = make_vector(args...); return make_vector(n, move(val));} /********* S E T **********/ template istream& operator >>(istream& stream, set& st) {T e; stream >> e; st.insert(e); return stream;} template ostream& operator <<(ostream& stream, const set& st) {if(st.size()){auto it=st.begin(); stream << *it++; for(; it!=st.end(); it++) cout << " " << *it;} return stream;} template inline T Max(const set& st) {assert(st.size()); return *prev(st.end());} template inline T Min(const set& st) {assert(st.size()); return *st.begin();} template inline bool Contains(const set& st, T x) {return st.find(x) != st.end();} /********* M U L T I S E T **********/ template istream& operator >>(istream& stream, multiset& st) {T e; stream >> e; st.insert(e); return stream;} template ostream& operator <<(ostream& stream, const multiset& st) {if(st.size()){auto it=st.begin(); stream << *it++; for(; it!=st.end(); it++) cout << " " << *it;} return stream;} template inline T Max(const multiset& st) {assert(st.size()); return *prev(st.end());} template inline T Min(const multiset& st) {assert(st.size()); return *st.begin();} template inline bool Contains(const multiset& st, T x) {return st.find(x) != st.end();} template inline bool EraseOne(multiset& st, T x) {auto it=st.find(x); if (it!=st.end()) {st.erase(it); return true;} else return false;} // 要素xを1つ削除. 消せたらtrueを返す. /********* S T R I N G **********/ template inline bool Contains(const string& s, const T& c) {return s.find(c) != string::npos;} // cはchar, string共に可 inline string ToUpper(const string& s) {string t; t.resize(s.size()); std::transform(all(s), t.begin(), ::toupper); return t; } inline string ToLower(const string& s) {string t; t.resize(s.size()); std::transform(all(s), t.begin(), ::tolower); return t; } /********* T R I O **********/ template struct trio { T1 first; T2 second; T3 third; // コンストラクタ・代入演算子 trio() {first=T1(); second=T2(); third=T3();} trio(const T1& x) : first(x), second(x), third(x) {} trio(const T1& x, const T2& y, const T3& z) : first(x), second(y), third(z) {} trio(const trio& t) {first=t.first; second=t.second; third=t.third;} // コピーコンストラクタ trio& operator =(const trio& t) {first=t.first; second=t.second; third=t.third; return *this;} // 比較演算子 bool operator <(const trio& t) const {return tie(first, second, third) < tie(t.first, t.second, t.third);} bool operator ==(const trio& t) const {return tie(first, second, third) == tie(t.first, t.second, t.third);} bool operator !=(const trio& other) const { return !(*this == other); } bool operator >(const trio& other) const { return other < *this; } bool operator <=(const trio& other) const { return !(*this > other); } bool operator >=(const trio& other) const { return !(*this < other); } // 単項演算子(+ -) trio operator +() const { return *this; } trio operator -() const { return (-1) * (*this); } // 複合代入演算子 trio& operator +=(const trio& t) {first += t.first; second += t.second; third += t.third; return *this;} trio& operator -=(const trio& t) {first -= t.first; second -= t.second; third -= t.third; return *this;} trio& operator *=(const trio& t) {first *= t.first; second *= t.second; third *= t.third; return *this;} trio& operator /=(const trio& t) {first /= t.first; second /= t.second; third /= t.third; return *this;} trio& operator %=(const trio& t) {first %= t.first; second %= t.second; third %= t.third; return *this;} // 算術演算子 friend trio operator +(const trio& lhs, const trio& rhs) {return trio(lhs) += rhs;} friend trio operator -(const trio& lhs, const trio& rhs) {return trio(lhs) -= rhs;} friend trio operator *(const trio& lhs, const trio& rhs) {return trio(lhs) *= rhs;} friend trio operator /(const trio& lhs, const trio& rhs) {return trio(lhs) /= rhs;} friend trio operator %(const trio& lhs, const trio& rhs) {return trio(lhs) %= rhs;} // 入出力 friend istream& operator >>(istream &stream, trio& t) {return stream >> t.first >> t.second >> t.third;} friend ostream& operator <<(ostream &stream, const trio& t) {return stream << t.first << " " << t.second << " " << t.third;} }; using tll = trio; /********* P R I N T **********/ template inline void print(const T& e) {cout << e << '\n';} template inline void print(const H& h, const T&... t) {cout << h << ' '; print(t...);} template inline void End(const T&... t) {print(t...); exit(0);} /********* D E B U G **********/ #ifdef __LOCAL #define debug(...) if(DEBUG) do{cout << '[' << #__VA_ARGS__ << "] ";debug_(__VA_ARGS__);}while(0) #else #define debug(...) #endif void dbg_(const long long& e) {if (e == INF) cout << "INF"; else if (e == -INF) cout << "-INF"; else cout << e;} template void dbg_(const T& e) {cout << e;} template void dbg_(const pair& p) {cout << '('; dbg_(p.first); cout << ' '; dbg_(p.second); cout << ')';} template void dbg_(const trio& t) {cout << '('; dbg_(t.first); cout << ' '; dbg_(t.second); cout << ' '; dbg_(t.third); cout << ')';} template void debug_(const T& e) {dbg_(e); cout << '\n';} template void debug_(const vector& v) {if (v.size()){auto it=v.begin(); dbg_(*it++); for(; it!=v.end(); ++it){cout << ' '; dbg_(*it);}} cout << '\n';} template void debug_(const vector>& vv) {cout << '\n'; ll cnt=0; for(auto&& v : vv){cout << cnt++ << ": "; debug_(v);}} template void debug_(const map& mp) {if (mp.size()) {auto it = mp.begin(); dbg_(*it++); for(; it != mp.end(); ++it) {cout << ' '; dbg_(*it);}} cout << '\n';} template void debug_(const vector>& vm){cout << '\n'; ll cnt=0; for(auto&& mp : vm){cout << cnt++ << ": "; debug_(mp);}} template void debug_(const set& st) {if(st.size()){auto it=st.begin(); dbg_(*it++); for(; it!=st.end(); ++it) {cout << ' '; dbg_(*it);}}cout << '\n';} template void debug_(const multiset& st) {if(st.size()) {auto it=st.begin(); dbg_(*it++); for(; it != st.end(); ++it) {cout << ' '; dbg_(*it);}} cout << '\n';} template void debug_(const vector>& vs) {cout << '\n'; ll cnt=0; for(auto&& st : vs){cout << cnt++ << ": "; debug_(st);}} template void debug_(const vector>& vs) {cout << '\n'; ll cnt=0;for(auto&& st : vs){cout << cnt++ << ": "; debug_(st);}} template void debug_(const H& h, const T&... t) {dbg_(h); cout << ", "; debug_(t...);} /********* O T H E R S **********/ template inline bool chmin(T& a, U b) {if (a > b) {a = b; return true;} return false;} // bは値渡し! template inline bool chmax(T& a, U b) {if (a < b) {a = b; return true;} return false;} template inline auto Mod(const T& a, const U& m) {return (a % m + m) % m;} // 負もOK template inline auto Ceil(const T& x, const U& y) {return x < 0 ? x/y : (x + y - 1) / y;} // 負もOK template inline auto Floor(const T& x, const U& y) {return -(Ceil(-x, y)); } // 負もOK inline ll isqrt(ll n) {assert(n >= 0); ll x = round(sqrt(n)); while(x * x > n) --x; return x;} template inline T Pow(T x, ll n) {assert(n >= 0); T ret = 1; while(1) {if (n % 2) ret *= x; n /= 2; if(!n) return ret; x = x * x;}} // べき乗 mintもOK template inline T Aseries(T a, T d, ll n) {assert(n >= 0); return a * n + n * (n - 1) / 2 * d;} // 等差級数 mintもOK template inline T Gseries(T a, T r, ll n) {assert(n >= 0); if (r == 1) return a * n; else return a * (1 - Pow(r, n)) / (1 - r);} // 等比級数 mintもOK template using min_priority_queue = priority_queue, greater>; inline bool Bit(ll b, int i) {assert(0 <= i && i < 64); return (b >> i) & 1;} inline ll Popcount(ll b) {return __builtin_popcountll(b);} inline ll Mask(ll n) {assert(0 <= n && n < 63); return (1LL << n) - 1LL;} // [0, n)のbitが立った64bit整数を返す。 inline ll Mask(ll n, ll m) {assert(n >= m); return Mask(n) ^ Mask(m);} // [m, n)のbitが立った64bit整数を返す。 /********* M O D **********/ // #include // https://atcoder.github.io/ac-library/production/document_ja/ // using namespace atcoder; // using mint = modint998244353; // modint1000000007; // istream& operator >>(istream& stream, mint& e) {ll n; stream >> n; e = n; return stream; } // ostream& operator <<(ostream& stream, const mint& e) { stream << e.val(); return stream; } // using vm = vector; // using vvm = vector; /***************************************/ constexpr bool DEBUG = false; // AOJでverify済 https://onlinejudge.u-aizu.ac.jp/problems/ALDS1_14_B // RollingHashを計算 // 参考:https://qiita.com/keymoon/items/11fac5627672a6d6a9f6 struct RollingHash { using int128_t = __int128_t; // GCC // using int128_t = boost::multiprecision::int128_t; // Boost const ll P = 61; const int128_t BASE = 368789449308LL; // 基数, 2^61-1の原始根 const int128_t MOD = (1LL << P) - 1; // modulo, 法 ll N; // 文字列/配列の長さ vector B; // B[i] : BASE ^ i; vector H; // H[a] : S[0..a](先頭a文字)のハッシュ値 // (a + b) % MODを計算 (a, bは 0 以上 MOD 未満) ll Add(ll a, ll b) const { assert(0 <= a && a < MOD && 0 <= b && b < MOD); ll ret = a + b; return (ret >= MOD) ? ret - MOD : ret; } // (a * b) % MOD を計算 int128_t Mul(int128_t a, int128_t b) const { int128_t t = a * b; // u, v < 2^Pとして、t = u * 2^P + v ≡ u + v mod. 2^P - 1 (∵ 2^P ≡ 1) int128_t ret = (t >> P) + (t & MOD); return (ret >= MOD) ? ret - MOD : ret; } RollingHash() {} RollingHash(const string& S, char c=0) { N = len(S); B.resize(N + 1); B[0] = 1; H.resize(N + 1); H[0] = 0; rep(i, 1, N + 1) B[i] = Mul(B[i - 1], BASE); rep(i, 1, N + 1) assert(S[i - 1] > c); rep(i, 1, N + 1) H[i] = Add(Mul(H[i - 1], BASE), S[i - 1] - c); } RollingHash(const vl& A) { N = len(A); B.resize(N + 1); B[0] = 1; H.resize(N + 1); H[0] = 0; rep(i, 1, N + 1) B[i] = Mul(B[i - 1], BASE); rep(i, 1, N + 1) assert(A[i - 1] > 0); rep(i, 1, N + 1) H[i] = Add(Mul(H[i - 1], BASE), A[i - 1]); } // 代入演算子 RollingHash& operator =(const RollingHash& other) { N = other.N; B = other.B; H = other.H; return *this; } // S[a:b]のハッシュ値, ret = (H[b] - H[a] * B[b - a]) % MOD ll operator() (ll a, ll b) const { assert(0 <= a && a <= b && b <= N); auto tmp = Mul(H[a], B[b - a]); ll ret = Add(H[b], Mul(MOD - 1, tmp)); // MOD - 1 ≡ -1 return ret; } // S[a1:b1] + S[a2:b2]のハッシュ値. ll operator() (ll a1, ll b1, ll a2, ll b2) const { assert(0 <= a1 && a1 <= b1 && b1 <= a2 && a2 <= b2 && b2 <= N); ll ret = Add(Mul((*this)(a1, b1), B[b2 - a2]), (*this)(a2, b2)); return ret; } // Sのハッシュ値 ll operator() () const { return (*this)(0, N); } // 左シフト(巡回), S[i:N] + S[0:i] ll lshift(ll i) const { ll j = Mod(i, N); ll ret = Add(Mul((*this)(j, N), B[j]), (*this)(0, j)); return ret; } // 右シフト(巡回), S[N-i:N] + S[0:N-i] ll rshift(ll i) const { return lshift(N - i); } }; // string S = "abcde"; // RollingHash rh(S); // ll h1 = rh(1, 3); // S[1:3] (ab)のハッシュ値 // ll h2 = rh(0, 2, 3, 4); // S[0:2] + S[3:4](abd) // ll h3 = rh(); // S(abcde) // ll h4 = rh.lshift(2); // S[2:] + s[:2](cdeab) // ll h5 = rh.rshift(2); // S[3:] + s[:3](deabc) int main() { cin.tie(nullptr); cout << fixed << setprecision(10); string S; cin >> S; ll N = len(S); auto rS(S); reverse(all(rS)); RollingHash rh1(S), rh2(rS); auto P = make_vector(N + 1, N + 1, false); rep(l, N + 1) { rep(r, l + 1, N + 1) { ll l2 = N - r; ll r2 = N - l; if (rh1(l, r) == rh2(l2, r2)) P[l][r] = true; } } debug(P); auto dp = make_vector(N + 1, N + 1, 0); auto seen = make_vector(N + 1, N + 1, false); auto dfs = [&](auto&& f, ll l, ll r) -> ll { if (seen[l][r]) return dp[l][r]; seen[l][r] = true; if (P[l][r]) { return dp[l][r] = r - l; } ll ret = 0; rep(m, l + 1, r) { ll n1 = f(f, l, m); ll n2 = f(f, m, r); chmax(ret, min(n1, n2)); } return dp[l][r] = ret; }; ll ans = dfs(dfs, 0, N); debug(dp); print(ans); }