fn lower_bound(val: usize, target: &Vec) -> usize { let mut lower = 0usize; let mut upper = target.len(); while upper > lower { let middle = (upper + lower) / 2; if val <= target[middle] { upper = middle; } else { lower = middle+1; } } upper } fn main() { let mut s = String::new(); std::io::stdin().read_line(&mut s).ok(); let s = s.trim().chars().map(|c| c as usize - 'a' as usize).collect::>(); let n = s.len(); let total_pattern = n * (n + 1) / 2; let mut cnt = 0usize; let mut patterns = vec![vec![]; 26]; for i in 0..n { patterns[s[i]].push(i); } for i in 0..26 { for j in 0..n { if s[j] == i { cnt += n - j; continue; } let lidx = lower_bound(j, &patterns[i]); if lidx == patterns[i].len() { continue; } let lidx = patterns[i][lidx]; cnt += n - lidx; } } println!("{}", cnt as f64 / total_pattern as f64); }