use std::error::Error; use std::io::{stdin}; use std::iter::FromIterator; fn f() -> Result<(), Box> { let mut s = String::new(); stdin().read_line(&mut s)?; let n: u32 = s.trim().parse()?; for _ in 0..n { let mut s = String::new(); stdin().read_line(&mut s)?; let s = s.trim(); println!("{}", g(s)); } Ok(()) } fn g(s: &str) -> String { let b = s.as_bytes(); let mut num_end = (b.len() - 1) as isize; while num_end >= 0 { if '0' as u8 <= b[num_end as usize] && b[num_end as usize] <= '9' as u8 { break; } num_end -= 1; } if num_end < 0 { return s.to_owned(); } // b[num_end] is number. let mut num_unmodified = num_end - 1; let num_end = num_end as usize; let mut num_buf = vec![]; if b[num_end] == '9' as u8 { num_buf.push('0' as u8); loop { match b[num_unmodified as usize] { x if x == '9' as u8 => { num_buf.push('0' as u8); num_unmodified -= 1; }, x if '0' as u8 <= x && x <= '8' as u8 => { num_buf.push(x + 1); num_unmodified -= 1; break; } _ => { num_buf.push('1' as u8); break; } } if num_unmodified < 0 { num_buf.push('1' as u8); break; } } } else { num_buf.push(b[num_end] + 1); } let mut v = if num_unmodified >= 0 { Vec::from_iter(b[..num_unmodified as usize + 1].iter().cloned()) } else { Vec::new() }; v.extend(num_buf.into_iter().rev()); if num_end < b.len() - 1 { v.extend(b[num_end + 1..].iter().cloned()); } String::from_utf8(v).unwrap() } fn main() { f().unwrap(); }