結果

問題 No.1729 ~サンプルはちゃんと見て!~ 16進数と8進数(1)
ユーザー seinyanseinyan
提出日時 2021-11-13 18:11:03
言語 Rust
(1.77.0)
結果
AC  
実行時間 1 ms / 2,000 ms
コード長 1,960 bytes
コンパイル時間 1,883 ms
コンパイル使用メモリ 166,060 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-18 11:54:41
合計ジャッジ時間 3,360 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,384 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 0 ms
4,380 KB
testcase_23 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

fn main() {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).unwrap();
    s = s.trim().to_string();

    let octal = bin_to_octal(&hex_to_bin(&s));

    let mut countv = vec![0_u64; 8];
    let mut max = 0_u64;
    for num in octal.chars() {
        let index = (num as i32 - 48) as usize;
        countv[index] += 1;
        max = std::cmp::max(countv[index], max);
    }

    let mut ans = Vec::new();
    for (i, v) in countv.iter().enumerate() {
        if *v == max {
            ans.push(i);
        }
    }
    println!(
        "{}",
        ans.iter()
            .map(|x| x.to_string())
            .collect::<Vec<String>>()
            .join(" ")
    );
}

fn bin_to_octal(bin: &String) -> String {
    let mut bin = bin.chars().collect::<Vec<char>>();
    let mut octal: Vec<char> = Vec::new();
    let mut table = std::collections::HashMap::new();
    for (v, k) in "01234567".chars().enumerate() {
        table.insert(
            format!("{:03b}", k as i32 - 48),
            std::char::from_digit(v as u32, 10).unwrap(),
        );
    }

    for _i in 0..(3 - bin.len() % 3) {
        bin.insert(0, '0');
    }

    for i in (0..bin.len() - 1).step_by(3) {
        let key = format!("{}{}{}", bin[i], bin[i + 1], bin[i + 2]);
        octal.push(*table.get(&key).unwrap());
    }

    loop {
        if octal[0] == '0' && octal.len() != 1 {
            octal.remove(0);
        } else {
            break;
        }
    }
    octal.iter().collect::<String>()
}

fn hex_to_bin(hex: &String) -> String {
    let hex = hex.to_uppercase();
    let vch: Vec<char> = hex.chars().collect();
    let mut bin = String::new();

    let mut table = std::collections::HashMap::new();
    for (i, num) in "0123456789ABCDEF".chars().enumerate() {
        table.insert(num, i as i32);
    }

    for ch in vch {
        assert!(ch.is_ascii_hexdigit());
        bin.push_str(&format!("{:04b}", table.get(&ch).unwrap()));
    }
    bin
}
0