結果

問題 No.539 インクリメント
ユーザー hatoohatoo
提出日時 2017-10-11 15:20:21
言語 Rust
(1.77.0)
結果
AC  
実行時間 35 ms / 2,000 ms
コード長 2,852 bytes
コンパイル時間 12,362 ms
コンパイル使用メモリ 383,704 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-28 16:06:04
合計ジャッジ時間 13,497 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 17 ms
6,816 KB
testcase_02 AC 35 ms
6,944 KB
testcase_03 AC 35 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#[allow(unused_imports)]
use std::cmp::{max, min};
#[allow(unused_imports)]
use std::collections::{HashMap, HashSet};

mod util {
    use std::io::stdin;
    use std::str::FromStr;
    use std::fmt::Debug;

    #[allow(dead_code)]
    pub fn line() -> String {
        let mut line: String = String::new();
        stdin().read_line(&mut line).unwrap();
        line.trim().to_string()
    }

    #[allow(dead_code)]
    pub fn get<T: FromStr>() -> T
    where
        <T as FromStr>::Err: Debug,
    {
        let mut line: String = String::new();
        stdin().read_line(&mut line).unwrap();
        line.trim().parse().unwrap()
    }

    #[allow(dead_code)]
    pub fn gets<T: FromStr>() -> Vec<T>
    where
        <T as FromStr>::Err: Debug,
    {
        let mut line: String = String::new();
        stdin().read_line(&mut line).unwrap();
        line.split_whitespace()
            .map(|t| t.parse().unwrap())
            .collect()
    }

    #[allow(dead_code)]
    pub fn get2<T: FromStr, U: FromStr>() -> (T, U)
    where
        <T as FromStr>::Err: Debug,
        <U as FromStr>::Err: Debug,
    {
        let mut line: String = String::new();
        stdin().read_line(&mut line).unwrap();
        let mut iter = line.split_whitespace();
        (
            iter.next().unwrap().parse().unwrap(),
            iter.next().unwrap().parse().unwrap(),
        )
    }
}


#[allow(unused_macros)]
macro_rules! debug {
    ($x: expr) => {
        println!("{}: {:?}", stringify!($x), $x)
    }
}

fn leve_up(s: &[char]) -> String {
    let r = (0..s.len()).rev().find(|&i| s[i].is_digit(10));
    if let Some(r) = r {
        let l = (0..r + 1)
            .rev()
            .take_while(|&i| s[i].is_digit(10))
            .last()
            .unwrap();

        let s_level = s[l..r + 1].iter().cloned().collect::<String>();

        let mid = add1(&s_level.chars().collect::<Vec<_>>());

        s[0..l]
            .iter()
            .cloned()
            .chain(mid.iter().cloned())
            .chain(s[r + 1..].iter().cloned())
            .collect()

    } else {
        // no digit
        s.iter().cloned().collect()
    }
}

fn add1(n: &[char]) -> Vec<char> {
    let mut res = Vec::new();

    let mut up = true;
    for &c in n.iter().rev() {
        if !up {
            res.push(c);
        } else {
            let d = c.to_digit(10).unwrap();
            if d == 9 {
                res.push('0');
            } else {
                res.push(('0' as u8 + d as u8 + 1) as char);
                up = false;
            }
        }
    }
    if up {
        res.push('1');
    }

    res.iter().cloned().rev().collect()
}

fn main() {
    let n: usize = util::get();
    for _ in 0..n {
        let line = util::line();
        let s = line.chars().collect::<Vec<char>>();
        println!("{}", leve_up(&s));
    }
}
0