#line 1 "combined.cpp" #pragma region Macros #include using namespace std; template inline bool chmax(T &a, T b) { if(a < b) { a = b; return 1; } return 0; } template inline bool chmin(T &a, T b) { if(a > b) { a = b; return 1; } return 0; } #ifdef DEBUG template ostream &operator<<(ostream &os, const pair &p) { os << '(' << p.first << ',' << p.second << ')'; return os; } template ostream &operator<<(ostream &os, const vector &v) { os << '{'; for(int i = 0; i < (int)v.size(); i++) { if(i) { os << ','; } os << v[i]; } os << '}'; return os; } void debugg() { cerr << endl; } template void debugg(const T &x, const Args &... args) { cerr << " " << x; debugg(args...); } #define debug(...) \ cerr << __LINE__ << " [" << #__VA_ARGS__ << "]: ", debugg(__VA_ARGS__) #define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl #else #define debug(...) (void(0)) #define dump(x) (void(0)) #endif struct Setup { Setup() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(15); } } __Setup; using ll = long long; #define OVERLOAD3(_1, _2, _3, name, ...) name #define ALL(v) (v).begin(), (v).end() #define RALL(v) (v).rbegin(), (v).rend() #define REP1(i, n) for(int i = 0; i < int(n); i++) #define REP2(i, a, b) for(int i = (a); i < int(b); i++) #define REP(...) OVERLOAD3(__VA_ARGS__, REP2, REP1)(__VA_ARGS__) #define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end()) #define SZ(v) ((int)(v).size()) const int INF = 1 << 30; const ll LLINF = 1LL << 60; constexpr int MOD = 1000000007; constexpr int MOD2 = 998244353; const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; void Case(int i) { cout << "Case #" << i << ": "; } int popcount(int x) { return __builtin_popcount(x); } ll popcount(ll x) { return __builtin_popcountll(x); } #pragma endregion Macros #line 1 "/Users/cbiolab/kyo-pro/compro_library/string/rolling_hash.hpp" // 最初にbaseをgenBase()でランダムに生成すること!!!!!!!!! // RollingHash(s, base) := 文字列sのハッシュテーブルを計算する // get(l, r) := [l, r)のハッシュ値を求める // connect(h1, h2, h2len) := ハッシュ値h1と, 長さh2lenのハッシュ値h2を結合する。 // LCP(b, l1, r1, l2, r2) := 区間[l1, r1)と、ハッシュテーブルがbである区間[l2, // r2)の文字列の 最長共通接頭辞の長さを求める。 struct RollingHash { using ull = unsigned long long; using ui128 = __uint128_t; static const ull mod = (1ULL << 61) - 1; const ull base; vector hashed, power; inline ull add(ull a, ull b) { if((a += b) >= mod) { a -= mod; } return a; } inline ull mul(ull a, ull b) { ui128 t = (ui128)a * b; ull na = t >> 61; ull nb = t & mod; if((na += nb) >= mod) { na -= mod; } return na; } static inline ull genBase() { random_device seed_gen; mt19937_64 engine(seed_gen()); uniform_int_distribution rand(2, mod - 2); return rand(engine); } RollingHash() = default; RollingHash(const string &s, ull base) : base(base) { int n = (int)s.size(); hashed.assign(n + 1, 0); power.assign(n + 1, 0); power[0] = 1; for(int i = 0; i < n; i++) { power[i + 1] = mul(power[i], base); hashed[i + 1] = add(mul(hashed[i], base), s[i]); } } ull get(int l, int r) { return add(hashed[r], mod - mul(hashed[l], power[r - l])); } ull connect(ull h1, ull h2, int h2len) { return add(mul(h1, power[h2len]), h2); } int LCP(RollingHash &b, int l1, int r1, int l2, int r2) { assert(mod == b.mod); int len = min(r1 - l1, r2 - l2); int low = -1, high = len + 1; while(high - low > 1) { int mid = (low + high) >> 1; if(get(l1, l1 + mid) == b.get(l2, l2 + mid)) { low = mid; } else { high = mid; } } return low; } }; #line 77 "combined.cpp" // abcdefg -> gfedcba void solve() { string s, rs; cin >> s; rs = s; reverse(ALL(rs)); auto BASE = RollingHash::genBase(); RollingHash r1(s, BASE), r2(rs, BASE); int N = SZ(s); vector dp(N+1, -1); REP(i, 1, N+1) if(r1.get(0, i) == r2.get(N-i, N)) dp[i] = i; REP(i, 1, N) { if(dp[i] == -1) continue; REP(j, i+1, N+1) { if(r1.get(i, j) == r2.get(N-j, N-i)) { chmax(dp[j], min(dp[i], j - i)); } } } cout << dp[N] << endl; } int main() { int T = 1; // cin >> T; while(T--) solve(); }