結果

問題 No.927 Second Permutation
ユーザー phsplsphspls
提出日時 2020-01-07 01:01:01
言語 Rust
(1.77.0)
結果
AC  
実行時間 25 ms / 2,000 ms
コード長 1,190 bytes
コンパイル時間 14,122 ms
コンパイル使用メモリ 378,636 KB
実行使用メモリ 8,064 KB
最終ジャッジ日時 2024-05-02 11:10:46
合計ジャッジ時間 15,757 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 0 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 1 ms
6,944 KB
testcase_07 AC 1 ms
6,944 KB
testcase_08 AC 9 ms
6,944 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 13 ms
6,940 KB
testcase_11 AC 22 ms
7,296 KB
testcase_12 AC 11 ms
6,940 KB
testcase_13 AC 3 ms
6,940 KB
testcase_14 AC 15 ms
6,940 KB
testcase_15 AC 10 ms
6,940 KB
testcase_16 AC 23 ms
7,936 KB
testcase_17 AC 25 ms
8,064 KB
testcase_18 AC 22 ms
7,932 KB
testcase_19 AC 23 ms
7,936 KB
testcase_20 AC 22 ms
7,936 KB
testcase_21 AC 22 ms
7,936 KB
testcase_22 AC 22 ms
8,064 KB
testcase_23 AC 22 ms
7,900 KB
testcase_24 AC 6 ms
6,940 KB
testcase_25 AC 4 ms
6,940 KB
testcase_26 AC 3 ms
6,944 KB
testcase_27 AC 3 ms
6,940 KB
testcase_28 AC 5 ms
6,940 KB
testcase_29 AC 5 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::io::Read;
use std::cmp::Reverse;
use std::collections::HashMap;

fn solve(x: &str) {
    let mut x_list: Vec<char> = x.chars().collect();
    x_list.sort_by_key(|&x| Reverse(x));

    let mut char2count: HashMap<char, usize> = HashMap::new();
    let mut min_key: u8 = 9;

    x_list.iter().for_each(|x| {
        min_key = std::cmp::min(min_key, x.to_string().parse::<u8>().unwrap());
        match char2count.get_mut(x) {
            Some(count) => *count += 1,
            None => { char2count.insert(*x, 1); },
        }
    });

    let min_key_char = &(std::char::from_digit(min_key as u32, 10).unwrap());
    let change_idx: usize = x_list.len() - char2count.get(min_key_char).unwrap();
    if x_list[0] == '0' || char2count.len() == 1 {
        println!("{}", -1);
    } else if min_key == 0 && change_idx == 1 {
        println!("{}", -1);
    } else {
        x_list.swap(change_idx - 1, change_idx);
        println!("{}", x_list.iter().map(|c| c.to_string()).collect::<Vec<String>>().join(""));
    }
}

fn main() {
    let mut x = String::new();
    std::io::stdin().read_to_string(&mut x).ok();
    let x = x.trim().split('\n').next().unwrap().trim();
    solve(x);
}
0