結果
| 問題 |
No.927 Second Permutation
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-01-07 01:01:01 |
| 言語 | Rust (1.83.0 + proconio) |
| 結果 |
AC
|
| 実行時間 | 22 ms / 2,000 ms |
| コード長 | 1,190 bytes |
| コンパイル時間 | 14,033 ms |
| コンパイル使用メモリ | 378,528 KB |
| 実行使用メモリ | 8,064 KB |
| 最終ジャッジ日時 | 2024-11-23 00:29:50 |
| 合計ジャッジ時間 | 15,774 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 27 |
ソースコード
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);
}