#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using lint = long long; using pint = pair; using plint = pair; struct fast_ios { fast_ios(){ cin.tie(nullptr), ios::sync_with_stdio(false), cout << fixed << setprecision(20); }; } fast_ios_; #define ALL(x) (x).begin(), (x).end() #define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i=i##_begin_;i--) #define REP(i, n) FOR(i,0,n) #define IREP(i, n) IFOR(i,0,n) template bool chmax(T &m, const T q) { return m < q ? (m = q, true) : false; } template bool chmin(T &m, const T q) { return m > q ? (m = q, true) : false; } const std::vector> grid_dxs{{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; int floor_lg(long long x) { return x <= 0 ? -1 : 63 - __builtin_clzll(x); } template T1 floor_div(T1 num, T2 den) { return (num > 0 ? num / den : -((-num + den - 1) / den)); } template std::pair operator+(const std::pair &l, const std::pair &r) { return std::make_pair(l.first + r.first, l.second + r.second); } template std::pair operator-(const std::pair &l, const std::pair &r) { return std::make_pair(l.first - r.first, l.second - r.second); } template std::vector sort_unique(std::vector vec) { sort(vec.begin(), vec.end()), vec.erase(unique(vec.begin(), vec.end()), vec.end()); return vec; } template int arglb(const std::vector &v, const T &x) { return std::distance(v.begin(), std::lower_bound(v.begin(), v.end(), x)); } template int argub(const std::vector &v, const T &x) { return std::distance(v.begin(), std::upper_bound(v.begin(), v.end(), x)); } template IStream &operator>>(IStream &is, std::vector &vec) { for (auto &v : vec) is >> v; return is; } template OStream &operator<<(OStream &os, const std::vector &vec); template OStream &operator<<(OStream &os, const std::array &arr); template OStream &operator<<(OStream &os, const std::unordered_set &vec); template OStream &operator<<(OStream &os, const pair &pa); template OStream &operator<<(OStream &os, const std::deque &vec); template OStream &operator<<(OStream &os, const std::set &vec); template OStream &operator<<(OStream &os, const std::multiset &vec); template OStream &operator<<(OStream &os, const std::unordered_multiset &vec); template OStream &operator<<(OStream &os, const std::pair &pa); template OStream &operator<<(OStream &os, const std::map &mp); template OStream &operator<<(OStream &os, const std::unordered_map &mp); template OStream &operator<<(OStream &os, const std::tuple &tpl); template OStream &operator<<(OStream &os, const std::vector &vec) { os << '['; for (auto v : vec) os << v << ','; os << ']'; return os; } template OStream &operator<<(OStream &os, const std::array &arr) { os << '['; for (auto v : arr) os << v << ','; os << ']'; return os; } template std::istream &operator>>(std::istream &is, std::tuple &tpl) { std::apply([&is](auto &&... args) { ((is >> args), ...);}, tpl); return is; } template OStream &operator<<(OStream &os, const std::tuple &tpl) { os << '('; std::apply([&os](auto &&... args) { ((os << args << ','), ...);}, tpl); return os << ')'; } template OStream &operator<<(OStream &os, const std::unordered_set &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template OStream &operator<<(OStream &os, const std::deque &vec) { os << "deq["; for (auto v : vec) os << v << ','; os << ']'; return os; } template OStream &operator<<(OStream &os, const std::set &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template OStream &operator<<(OStream &os, const std::multiset &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template OStream &operator<<(OStream &os, const std::unordered_multiset &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template OStream &operator<<(OStream &os, const std::pair &pa) { return os << '(' << pa.first << ',' << pa.second << ')'; } template OStream &operator<<(OStream &os, const std::map &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; } template OStream &operator<<(OStream &os, const std::unordered_map &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; } #ifdef HITONANODE_LOCAL const string COLOR_RESET = "\033[0m", BRIGHT_GREEN = "\033[1;32m", BRIGHT_RED = "\033[1;31m", BRIGHT_CYAN = "\033[1;36m", NORMAL_CROSSED = "\033[0;9;37m", RED_BACKGROUND = "\033[1;41m", NORMAL_FAINT = "\033[0;2m"; #define dbg(x) std::cerr << BRIGHT_CYAN << #x << COLOR_RESET << " = " << (x) << NORMAL_FAINT << " (L" << __LINE__ << ") " << __FILE__ << COLOR_RESET << std::endl #define dbgif(cond, x) ((cond) ? std::cerr << BRIGHT_CYAN << #x << COLOR_RESET << " = " << (x) << NORMAL_FAINT << " (L" << __LINE__ << ") " << __FILE__ << COLOR_RESET << std::endl : std::cerr) #else #define dbg(x) ((void)0) #define dbgif(cond, x) ((void)0) #endif // Manacher's Algorithm: radius of palindromes // Input: std::string or std::vector of length N // Output: std::vector of size N // Complexity: O(N) // Sample: // - `sakanakanandaka` -> [1, 1, 2, 1, 4, 1, 4, 1, 2, 2, 1, 1, 1, 2, 1] // Reference: https://snuke.hatenablog.com/entry/2014/12/02/235837 template std::vector manacher(const std::vector &S) { std::vector res(S.size()); int i = 0, j = 0; while (i < int(S.size())) { while (i - j >= 0 and i + j < int(S.size()) and S[i - j] == S[i + j]) j++; res[i] = j; int k = 1; while (i - k >= 0 and i + k < int(S.size()) and k + res[i - k] < j) res[i + k] = res[i - k], k++; i += k, j -= k; } return res; } std::vector manacher(const std::string &S) { std::vector v(S.size()); for (int i = 0; i < int(S.size()); i++) v[i] = S[i]; return manacher(v); } template std::vector> enumerate_palindromes(const std::vector &vec) { std::vector v; const int N = vec.size(); for (int i = 0; i < N - 1; i++) { v.push_back(vec[i]); v.push_back(-1); } v.push_back(vec.back()); const auto man = manacher(v); std::vector> ret; for (int i = 0; i < N * 2 - 1; i++) { if (i & 1) { int w = man[i] / 2; ret.emplace_back((i + 1) / 2 - w, (i + 1) / 2 + w); } else { int w = (man[i] - 1) / 2; ret.emplace_back(i / 2 - w, i / 2 + w + 1); } } return ret; } std::vector> enumerate_palindromes(const std::string &S) { std::vector v(S.size()); for (int i = 0; i < int(S.size()); i++) v[i] = S[i]; return enumerate_palindromes(v); } // F_p, p = 2^61 - 1 // https://qiita.com/keymoon/items/11fac5627672a6d6a9f6 class ModIntMersenne61 { static const long long md = (1LL << 61) - 1; long long _v; inline unsigned hi() const noexcept { return _v >> 31; } inline unsigned lo() const noexcept { return _v & ((1LL << 31) - 1); } public: static long long mod() { return md; } ModIntMersenne61() : _v(0) {} // 0 <= x < md * 2 explicit ModIntMersenne61(long long x) : _v(x >= md ? x - md : x) {} long long val() const noexcept { return _v; } ModIntMersenne61 operator+(const ModIntMersenne61 &x) const { return ModIntMersenne61(_v + x._v); } ModIntMersenne61 operator-(const ModIntMersenne61 &x) const { return ModIntMersenne61(_v + md - x._v); } ModIntMersenne61 operator*(const ModIntMersenne61 &x) const { using ull = unsigned long long; ull uu = (ull)hi() * x.hi() * 2; ull ll = (ull)lo() * x.lo(); ull lu = (ull)hi() * x.lo() + (ull)lo() * x.hi(); ull sum = uu + ll + ((lu & ((1ULL << 30) - 1)) << 31) + (lu >> 30); ull reduced = (sum >> 61) + (sum & ull(md)); return ModIntMersenne61(reduced); } ModIntMersenne61 pow(long long n) const { assert(n >= 0); ModIntMersenne61 ans(1), tmp = *this; while (n) { if (n & 1) ans *= tmp; tmp *= tmp, n >>= 1; } return ans; } ModIntMersenne61 inv() const { return pow(md - 2); } ModIntMersenne61 operator/(const ModIntMersenne61 &x) const { return *this * x.inv(); } ModIntMersenne61 operator-() const { return ModIntMersenne61(md - _v); } ModIntMersenne61 &operator+=(const ModIntMersenne61 &x) { return *this = *this + x; } ModIntMersenne61 &operator-=(const ModIntMersenne61 &x) { return *this = *this - x; } ModIntMersenne61 &operator*=(const ModIntMersenne61 &x) { return *this = *this * x; } ModIntMersenne61 &operator/=(const ModIntMersenne61 &x) { return *this = *this / x; } ModIntMersenne61 operator+(unsigned x) const { return ModIntMersenne61(this->_v + x); } bool operator==(const ModIntMersenne61 &x) const { return _v == x._v; } bool operator!=(const ModIntMersenne61 &x) const { return _v != x._v; } bool operator<(const ModIntMersenne61 &x) const { return _v < x._v; } // To use std::map template friend OStream &operator<<(OStream &os, const ModIntMersenne61 &x) { return os << x._v; } static ModIntMersenne61 randgen(bool force_update = false) { static ModIntMersenne61 b(0); if (b == ModIntMersenne61(0) or force_update) { std::mt19937 mt(std::chrono::steady_clock::now().time_since_epoch().count()); std::uniform_int_distribution d(1, ModIntMersenne61::mod()); b = ModIntMersenne61(d(mt)); } return b; } }; #include #include #include #include #include #include template struct PairHash : public std::pair { using PH = PairHash; explicit PairHash(T1 x, T2 y) : std::pair(x, y) {} explicit PairHash(int x) : std::pair(x, x) {} PairHash() : PairHash(0) {} PH operator+(const PH &x) const { return PH(this->first + x.first, this->second + x.second); } PH operator-(const PH &x) const { return PH(this->first - x.first, this->second - x.second); } PH operator*(const PH &x) const { return PH(this->first * x.first, this->second * x.second); } PH operator+(int x) const { return PH(this->first + x, this->second + x); } static PH randgen(bool force_update = false) { static PH b(0); if (b == PH(0) or force_update) { std::mt19937 mt(std::chrono::steady_clock::now().time_since_epoch().count()); std::uniform_int_distribution d(1 << 30); b = PH(T1(d(mt)), T2(d(mt))); } return b; } template friend OStream &operator<<(OStream &os, const PH &x) { return os << "(" << x.first << ", " << x.second << ")"; } }; template struct TupleHash3 : public std::tuple { using TH = TupleHash3; explicit TupleHash3(T1 x, T2 y, T3 z) : std::tuple(x, y, z) {} explicit TupleHash3(int x) : std::tuple(x, x, x) {} TupleHash3() : TupleHash3(0) {} inline const T1 &v1() const noexcept { return std::get<0>(*this); } inline const T2 &v2() const noexcept { return std::get<1>(*this); } inline const T3 &v3() const noexcept { return std::get<2>(*this); } TH operator+(const TH &x) const { return TH(v1() + x.v1(), v2() + x.v2(), v3() + x.v3()); } TH operator-(const TH &x) const { return TH(v1() - x.v1(), v2() - x.v2(), v3() - x.v3()); } TH operator*(const TH &x) const { return TH(v1() * x.v1(), v2() * x.v2(), v3() * x.v3()); } TH operator+(int x) const { return TH(v1() + x, v2() + x, v3() + x); } static TH randgen(bool force_update = false) { static TH b(0); if (b == TH(0) or force_update) { std::mt19937 mt(std::chrono::steady_clock::now().time_since_epoch().count()); std::uniform_int_distribution d(1 << 30); b = TH(T1(d(mt)), T2(d(mt)), T3(d(mt))); } return b; } template friend OStream &operator<<(OStream &os, const TH &x) { return os << "(" << x.v1() << ", " << x.v2() << ", " << x.v3() << ")"; } }; // Rolling Hash (Rabin-Karp), 1dim template struct rolling_hash { int N; const V B; std::vector hash; // hash[i] = s[0] * B^(i - 1) + ... + s[i - 1] static std::vector power; // power[i] = B^i void _extend_powvec() { if (power.size() > 1 and power.at(1) != B) power = {V(1)}; while (static_cast(power.size()) <= N) { auto tmp = power.back() * B; power.push_back(tmp); } } template rolling_hash(const std::vector &s, V b = V::randgen()) : N(s.size()), B(b), hash(N + 1) { for (int i = 0; i < N; i++) hash[i + 1] = hash[i] * B + s[i]; _extend_powvec(); } rolling_hash(const std::string &s = "", V b = V::randgen()) : N(s.size()), B(b), hash(N + 1) { for (int i = 0; i < N; i++) hash[i + 1] = hash[i] * B + s[i]; _extend_powvec(); } void addchar(const char &c) { V hnew = hash[N] * B + c; N++, hash.emplace_back(hnew); _extend_powvec(); } struct Hash { int length; V val; Hash() : length(0), val(V()) {} Hash(int len, const V &v) : length(len), val(v) {} bool operator==(const Hash &r) const noexcept { return length == r.length and val == r.val; } Hash operator*(const Hash &r) const { return Hash(length + r.length, val * power.at(r.length) + r.val); } template friend OStream &operator<<(OStream &os, const Hash &x) { return os << "(length=" << x.length << ", val=" << x.val << ")"; } }; Hash get(int l, int r) const { // s[l] * B^(r - l - 1) + ... + s[r - 1] if (l >= r) return Hash(); return Hash(r - l, hash[r] - hash[l] * power[r - l]); } int lcplen(int l1, int l2) const { return longest_common_prefix(*this, l1, *this, l2); } }; template std::vector rolling_hash::power{V(1)}; // Longest common prerfix between s1[l1, N1) and s2[l2, N2) template int longest_common_prefix(const rolling_hash &rh1, int l1, const rolling_hash &rh2, int l2) { int lo = 0, hi = std::min(rh1.N + 1 - l1, rh2.N + 1 - l2); while (hi - lo > 1) { const int c = (lo + hi) / 2; auto h1 = rh1.get(l1, l1 + c), h2 = rh2.get(l2, l2 + c); (h1 == h2 ? lo : hi) = c; } return lo; } // Longest common suffix between s1[0, r1) and s2[0, r2) template int longest_common_suffix(const rolling_hash &rh1, int r1, const rolling_hash &rh2, int r2) { int lo = 0, hi = std::min(r1, r2) + 1; while (hi - lo > 1) { const int c = (lo + hi) / 2; auto h1 = rh1.get(r1 - c, r1), h2 = rh2.get(r2 - c, r2); (h1 == h2 ? lo : hi) = c; } return lo; } struct Trie { char a_init; int D; int INVALID = -1; std::vector> child; std::vector par; using T_NODE = lint; std::vector v_info; Trie(char a_init = 'a', int D = 26) : a_init(a_init), D(D), child(1, std::vector(D, INVALID)), par(1, -1), v_info(1) {} int step(int now, char c) const { if (now == INVALID) return INVALID; return child.at(now).at(c - a_init); } int add_word(const std::string &str, T_NODE info) { int now = 0; for (auto &c : str) { if (child[now][c - a_init] == INVALID) { par.push_back(now); child[now][c - a_init] = child.size(); child.emplace_back(D, INVALID); v_info.resize(child.size()); } now = child[now][c - a_init]; } v_info[now] += info; return now; } }; int main() { string S; cin >> S; dbg(S); // auto Srev = S; // reverse(ALL(Srev)); rolling_hash rh(S); auto par = enumerate_palindromes(S); dbg(par); using P = pair; map mp; map mpw; map mpw_done; // map score; for (auto [l, r] : par) { if (l == r) continue; auto h = rh.get(l, r); // score[h.val] += r - l; mp[{r - l, h.val}] = {l, r}; mpw[{r - l, h.val}]++; } vector lrs; while (!mpw.empty()) { auto [key, v] = *prev(mpw.end()); mpw.erase(key); mpw_done[key] = v; auto [l, r] = mp.at(key); lrs.emplace_back(l, r); ++l; --r; if (l < r) { mp[{r - l, rh.get(l, r).val}] = {l, r}; mpw[{r - l, rh.get(l, r).val}] += v; } } reverse(ALL(lrs)); dbg(lrs); Trie trie('a', 26); for (auto [l, r] : lrs) { auto s = mpw_done.at({r - l, rh.get(l, r).val}) * (r - l); trie.add_word(S.substr(l, r - l), s); } lint ret = 0; auto rec = [&](auto &&self, int now, lint cur) -> void { cur += trie.v_info.at(now); chmax(ret, cur); for (int nxt : trie.child.at(now)) { if (nxt == trie.INVALID) continue; self(self, nxt, cur); } }; rec(rec, 0, 0); cout << ret << '\n'; }