結果

問題 No.10 +か×か
コンテスト
ユーザー tsubu_taiyaki
提出日時 2017-02-06 07:09:47
言語 Rust
(1.94.0 + proconio + num + itertools)
コンパイル:
/usr/bin/rustc_custom
実行:
./target/release/main
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,639 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 261 ms
コンパイル使用メモリ 152,120 KB
最終ジャッジ日時 2026-05-10 07:40:31
合計ジャッジ時間 1,438 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge3_0
このコードへのチャレンジ
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。

コンパイルメッセージ
error[E0782]: expected a type, found a trait
  --> src/main.rs:10:21
   |
10 |     tokens: &'a mut Iterator<Item = String>,
   |                     ^^^^^^^^^^^^^^^^^^^^^^^
   |
help: you can add the `dyn` keyword if you want a trait object
   |
10 |     tokens: &'a mut dyn Iterator<Item = String>,
   |                     +++

error[E0782]: expected a type, found a trait
  --> src/main.rs:14:23
   |
14 |     fn new(i: &'a mut Iterator<Item = String>) -> Self {
   |                       ^^^^^^^^^^^^^^^^^^^^^^^
   |
help: use a new generic type parameter, constrained by `Iterator<Item = String>`
   |
14 -     fn new(i: &'a mut Iterator<Item = String>) -> Self {
14 +     fn new<T: Iterator<Item = String>>(i: &'a mut T) -> Self {
   |
help: you can also use an opaque type, but users won't be able to specify the type parameter when calling the `fn`, having to rely exclusively on type inference
   |
14 |     fn new(i: &'a mut impl Iterator<Item = String>) -> Self {
   |                       ++++
help: alternatively, use a trait object to accept any type that implements `Iterator<Item = String>`, accessing its methods at runtime using dynamic dispatch
   |
14 |     fn new(i: &'a mut dyn Iterator<Item = String>) -> Self {
   |                       +++

error[E0282]: type annotations needed
  --> src/main.rs:25:34
   |
25 |         self.tokens.take(n).map(|s| match s.parse() { Ok(x) => x, Err(_) => panic!() } ).collect()
   |                                  ^        - type must be known at this point
   |
help: consider giving this closure parameter an explicit type
   |
25 |         self.tokens.take(n).map(|s: /* Type */| match s.parse() { Ok(x) => x, Err(_) => panic!() } ).collect()
   |                                   ++++++++++++

Some errors have detailed explanations: E0282, E0782.
For more information about an error, try `rustc --explain E0282`.
error: could not compile `main` (bin "main") due to 3 previous errors

ソースコード

diff #
raw source code

#![allow(unused_imports)]

use std::io::{self, BufRead};
use std::str::FromStr;
use std::collections::*;
use std::cmp::*;

#[allow(dead_code)]
struct Parser<'a> {
    tokens: &'a mut Iterator<Item = String>,
}
#[allow(dead_code)]
impl<'a> Parser<'a> {
    fn new(i: &'a mut Iterator<Item = String>) -> Self {
        Parser {tokens: i}
    }
    fn take<T: FromStr>(&mut self) -> T {
        match self.tokens.next().expect("empty iterator").parse() {
            Ok(x) => x,
            Err(_) => panic!()
        }
    }

    fn take_some<T: FromStr>(&mut self, n: usize) -> Vec<T> {
        self.tokens.take(n).map(|s| match s.parse() { Ok(x) => x, Err(_) => panic!() } ).collect()
    }
}

//sieve[k] is true if k is a prime
//[0, n)
#[allow(dead_code)]
fn sieve_of_eratosthenes(n: u64) -> Vec<bool> {
    let mut v = vec![true; n as usize];
    v[0] = false;
    v[1] = false;
    for i in 2..n {
        if !v[i as usize] {
            continue;
        }
        let mut j = 2*i;
        while j < n {
            v[j as usize] = false;
            j += i;
        }
    }
    v
}

#[allow(dead_code)]
fn vec_2d<T>(n: usize, m: usize, t: T) -> Vec<Vec<T>> where T: Clone {
    let mut u = Vec::new();
    let mut v = Vec::new();
    v.resize(m, t);
    u.resize(n, v);
    u
}

fn main() {
    let stdin = io::stdin();
    let mut tokens = stdin.lock().lines().filter_map(|x| x.ok()).flat_map(|x| x.split_whitespace().map(|s| s.to_owned()).collect::<Vec<String>>());
    let mut parser = Parser::new(&mut tokens);

    let n:usize = parser.take();
    let total: usize = parser.take();
    let a: Vec<usize> = parser.take_some(n);

    let mut dp = vec_2d(n+1, total+1, ".".to_string());
    dp[0][a[0]] = "".to_string();
    for i in 1..n {
        for j in 0..(total+1) {
            let f1 = j >= a[i] && dp[i-1][j-a[i]] != ".";
            let f2 = j%a[i] == 0 && dp[i-1][j/a[i]] != ".";
            if !f1 && !f2 {
                continue;
            }

            let update = match (f1, f2, &dp[i-1][j-a[i]], &dp[i-1][j/a[i]]) {
                (true, true, s1, s2) => {
                    if s1 >= s2 {
                        format!("{}{}", s1, "+")
                    } else {
                        format!("{}{}", s2, "*")
                    }
                },
                (true, false, s1, _) => {
                    format!("{}{}", s1, "+")
                },
                (false, true, _, s2) => {
                    format!("{}{}", s2, "*")
                },
                _=> "".to_string()
            };
            dp[i][j] = update;
        }
    }
    println!("{}", dp[n-1][total]);
}


0