結果

問題 No.10 +か×か
ユーザー tabataba
提出日時 2024-07-31 13:33:12
言語 Rust
(1.77.0)
結果
RE  
実行時間 -
コード長 1,287 bytes
コンパイル時間 13,477 ms
コンパイル使用メモリ 401,924 KB
実行使用メモリ 28,132 KB
最終ジャッジ日時 2024-07-31 13:33:27
合計ジャッジ時間 15,255 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 0 ms
6,812 KB
testcase_01 AC 1 ms
6,812 KB
testcase_02 AC 1 ms
6,812 KB
testcase_03 AC 111 ms
28,132 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 RE -
testcase_06 AC 121 ms
28,072 KB
testcase_07 WA -
testcase_08 AC 6 ms
6,944 KB
testcase_09 AC 7 ms
6,940 KB
testcase_10 AC 1 ms
6,944 KB
testcase_11 AC 1 ms
6,944 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused imports: `BTreeSet`, `HashMap`, `VecDeque`, `io::stdin`, `str::FromStr`, `sync::OnceLock`
 --> src/main.rs:2:19
  |
2 |     collections::{BTreeSet, HashMap, HashSet, VecDeque},
  |                   ^^^^^^^^  ^^^^^^^           ^^^^^^^^
3 |     fmt::Debug,
4 |     io::stdin,
  |     ^^^^^^^^^
5 |     str::FromStr,
  |     ^^^^^^^^^^^^
6 |     sync::OnceLock,
  |     ^^^^^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

warning: unused import: `proconio::marker::Chars`
 --> src/main.rs:9:5
  |
9 | use proconio::marker::Chars;
  |     ^^^^^^^^^^^^^^^^^^^^^^^

ソースコード

diff #

use std::{
    collections::{BTreeSet, HashMap, HashSet, VecDeque},
    fmt::Debug,
    io::stdin,
    str::FromStr,
    sync::OnceLock,
};

use proconio::marker::Chars;

fn main() {
    proconio::input! {
        n: u32,
        t: u32,
        a: [u32; n],
    }
    let result = f(a[0], t, &mut Vec::new(), &a[1..], &mut HashSet::new()).unwrap();
    let result = result
        .into_iter()
        .map(|o| match o {
            Op::Plus => '+',
            Op::Mult => '*',
        })
        .collect::<String>();
    println!("{result}");
}

#[derive(Debug, Clone, Copy)]
enum Op {
    Plus,
    Mult,
}
fn f(
    v: u32,
    t: u32,
    op: &mut Vec<Op>,
    a: &[u32],
    memo: &mut HashSet<(u32, usize)>,
) -> Option<Vec<Op>> {
    if a.is_empty() {
        return (v == t).then(|| op.clone());
    }
    if memo.contains(&(v, a.len())) {
        return None;
    }
    if v < t {
        op.push(Op::Plus);
        let plus_v = v + a[0];
        if let Some(r) = f(plus_v, t, op, &a[1..], memo) {
            return Some(r);
        }
        op.pop();
        op.push(Op::Mult);
        let mult_v = v * a[0];
        if let Some(r) = f(mult_v, t, op, &a[1..], memo) {
            return Some(r);
        }
        op.pop();
    }
    memo.insert((v, a.len()));
    None
}
0