// O(|S|(log|S|)^2) // 節約できた回数をもつ // ---------- begin chmin, chmax ---------- pub trait ChangeMinMax { fn chmin(&mut self, x: Self) -> bool; fn chmax(&mut self, x: Self) -> bool; } impl ChangeMinMax for T { fn chmin(&mut self, x: Self) -> bool { *self > x && { *self = x; true } } fn chmax(&mut self, x: Self) -> bool { *self < x && { *self = x; true } } } // ---------- end chmin, chmax ---------- // ---------- begin z_algorithm ---------- fn z_algorithm(s: &[T]) -> Vec { let len = s.len(); let mut a = vec![0; len]; a[0] = len; let mut i = 1; let mut j = 0; while i < len { while i + j < len && s[j] == s[i + j] { j += 1; } a[i] = j; if j == 0 { i += 1; continue; } let mut k = 1; while i + k < len && k + a[k] < j { a[i + k] = a[k]; k += 1; } i += k; j -= k; } a } // ---------- end z_algorithm ---------- fn read() -> Vec { let mut s = String::new(); std::io::stdin().read_line(&mut s).unwrap(); let s = s.trim().chars().collect::>(); assert!(1 <= s.len() && s.len() <= 200000); assert!(s.iter().all(|s| "ab".contains(*s))); s } fn main() { let s = read(); let za = z_algorithm(&s); let mut set = std::collections::BTreeSet::new(); for i in 1..s.len() { if za[i] >= 1 { set.insert(i); } } let mut index = za.iter().enumerate().map(|p| (*p.1, p.0)).collect::>(); index.sort_by_key(|p| !p.0); let mut dp = vec![0i32; s.len() + 1]; for i in 1..s.len() { let d = dp[i]; dp[i + 1].chmax(d); let len = i as i32; let mut cnt = 0; let mut pos = i; while let Some(&x) = set.range(pos..).next() { cnt += 1; dp[x + i].chmax(d - 1 + cnt * (len - 1)); pos = x + i; } while index.last().map_or(false, |p| p.0 <= i) { let (_, x) = index.pop().unwrap(); set.remove(&x); } } let ans = s.len() as i32 - dp[s.len()]; println!("{}", ans); }