use proconio::input; fn main() { input! { (n, k): (usize, u64), t: String, } let encrypt = t .chars() .map(|ch| ch.to_digit(10).unwrap() as u64) .collect::>(); let mut suffix_num_combs = vec![0_u64; n + 2]; suffix_num_combs[n] = 1; for i in (0..n).rev() { let num_combs_1 = suffix_num_combs[i + 1] * valid_encryption(&encrypt[i..i + 1]) as u64; let num_combs_2 = suffix_num_combs[i + 2] * (i + 1 < n && valid_encryption(&encrypt[i..i + 2])) as u64; suffix_num_combs[i] = num_combs_1.saturating_add(num_combs_2); } let mut plaintext = String::new(); let mut overcome = 0_u64; let mut progress = 0_usize; while progress < n { if valid_encryption(&encrypt[progress..progress + 1]) && overcome + suffix_num_combs[progress + 1] >= k { plaintext.push((b'a' + encrypt[progress] as u8 - 1) as char); progress += 1; } else { plaintext .push((b'a' + (10 * encrypt[progress] + encrypt[progress + 1]) as u8 - 1) as char); overcome += suffix_num_combs[progress + 1]; progress += 2; } } println!("{plaintext}"); } fn valid_encryption(crypt: &[u64]) -> bool { match crypt.len() { 0 => true, 1 => crypt[0] != 0, 2 => crypt[0] != 0 && 10 * crypt[0] + crypt[1] <= 26, _ => false, } }