use std::collections::*; type Map = BTreeMap; fn main() { input!(s: chars); let mut tree = PalindromicTree::new(); for s in s { tree.add(s); } let tree = tree; let mut cnt = vec![0; tree.node.len()]; for p in tree.pos.iter() { cnt[*p] += 1; } for i in (2..cnt.len()).rev() { cnt[tree.node[i].fail] += cnt[i]; } let mut dp = vec![0; cnt.len()]; for i in 2..dp.len() { dp[i] = cnt[i] * tree.node[i].len + dp[tree.node[i].fail]; } println!("{}", dp.into_iter().max().unwrap()); } #[derive(Debug)] struct Node { fail: usize, len: usize, next: Map, } impl Node where T: Copy + Ord, { fn new(fail: usize, len: usize) -> Self { Self { fail, len, next: Map::new(), } } fn next(&self, x: T) -> Option { self.next.get(&x).cloned() } fn insert(&mut self, c: T, x: usize) { self.next.insert(c, x); } } pub struct PalindromicTree { s: Vec, pos: Vec, node: Vec>, } impl PalindromicTree where T: Copy + Ord, { pub fn new() -> Self { Self { s: vec![], pos: vec![], node: vec![Node::new(0, !0), Node::new(0, 0)], } } pub fn add(&mut self, c: T) { self.s.push(c); let all = self.s.len(); let now = self.pos.last().map_or(1, |p| *p); let mut x = now; while x > 0 { let k = self.node[x].len; if k + 2 <= all && self.s[all - k - 2] == c { break; } x = self.node[x].fail; } let len = self.node[x].len + 2; let mut fail = 1; if x > 0 { fail = self.node[now].fail; while fail > 0 { let k = self.node[fail].len; if self.node[fail].next(c).is_some() && self.s[all - k - 2] == c { break; } fail = self.node[fail].fail; } fail = self.node[fail].next(c).unwrap(); } if let Some(z) = self.node[x].next(c) { self.pos.push(z); } else { let y = self.node.len(); self.node[x].insert(c, y); self.node.push(Node::new(fail, len)); self.pos.push(y); } } } impl PalindromicTree where T: std::fmt::Debug, { pub fn enumerate(&self, x: usize) { println!("enumerate: {}", x); let mut pos = self.pos[x]; while pos > 1 { let len = self.node[pos].len; println!("{:?}", &self.s[(x - len + 1)..=x]); pos = self.node[pos].fail; } } } // ---------- begin input macro ---------- // reference: https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 #[macro_export] macro_rules! input { (source = $s:expr, $($r:tt)*) => { let mut iter = $s.split_whitespace(); input_inner!{iter, $($r)*} }; ($($r:tt)*) => { let s = { use std::io::Read; let mut s = String::new(); std::io::stdin().read_to_string(&mut s).unwrap(); s }; let mut iter = s.split_whitespace(); input_inner!{iter, $($r)*} }; } #[macro_export] macro_rules! input_inner { ($iter:expr) => {}; ($iter:expr, ) => {}; ($iter:expr, $var:ident : $t:tt $($r:tt)*) => { let $var = read_value!($iter, $t); input_inner!{$iter $($r)*} }; } #[macro_export] macro_rules! read_value { ($iter:expr, ( $($t:tt),* )) => { ( $(read_value!($iter, $t)),* ) }; ($iter:expr, [ $t:tt ; $len:expr ]) => { (0..$len).map(|_| read_value!($iter, $t)).collect::>() }; ($iter:expr, chars) => { read_value!($iter, String).chars().collect::>() }; ($iter:expr, bytes) => { read_value!($iter, String).bytes().collect::>() }; ($iter:expr, usize1) => { read_value!($iter, usize) - 1 }; ($iter:expr, $t:ty) => { $iter.next().unwrap().parse::<$t>().expect("Parse error") }; } // ---------- end input macro ----------