// 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 inf = s.len() + 1; 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![inf; s.len() + 1]; dp[0] = 0; dp[1] = 1; for i in 1..s.len() { let d = dp[i]; dp[i + 1].chmin(d + 1); let mut cnt = 0; let mut pos = i; while let Some(&x) = set.range(pos..).next() { cnt += 1; dp[x + i].chmin(d + x - cnt * i + cnt + 1); pos = x + i; } while index.last().map_or(false, |p| p.0 <= i) { let (_, x) = index.pop().unwrap(); set.remove(&x); } } println!("{}", dp[s.len()]); }