結果

問題 No.1196 A lazy student
ユーザー akakimidoriakakimidori
提出日時 2020-08-22 17:47:59
言語 Rust
(1.77.0)
結果
AC  
実行時間 58 ms / 1,000 ms
コード長 2,805 bytes
コンパイル時間 645 ms
コンパイル使用メモリ 169,068 KB
実行使用メモリ 73,420 KB
最終ジャッジ日時 2024-04-23 11:37:37
合計ジャッジ時間 1,366 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 0 ms
6,940 KB
testcase_03 AC 1 ms
6,944 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 1 ms
6,940 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 4 ms
6,940 KB
testcase_09 AC 8 ms
6,940 KB
testcase_10 AC 8 ms
6,940 KB
testcase_11 AC 8 ms
6,940 KB
testcase_12 AC 1 ms
6,940 KB
testcase_13 AC 0 ms
6,944 KB
testcase_14 AC 1 ms
6,940 KB
testcase_15 AC 58 ms
73,420 KB
testcase_16 AC 13 ms
15,152 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused variable: `n`
   --> main.rs:108:9
    |
108 |         n: usize,
    |         ^
    |
help: `n` is captured in macro and introduced a unused variable
   --> main.rs:23:13
    |
23  |           let $var = read_value!($iter, $t);
    |               ^^^^
...
107 | /     input! {
108 | |         n: usize,
109 | |         p: [f64; 3],
110 | |         s: bytes,
111 | |     }
    | |_____- in this macro invocation
    = note: `#[warn(unused_variables)]` on by default
    = note: this warning originates in the macro `input_inner` which comes from the expansion of the macro `input` (in Nightly builds, run with -Z macro-backtrace for more info)

warning: 1 warning emitted

ソースコード

diff #

//https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 より
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};
    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };
    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };
    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };
    ($iter:expr, bytes) => {
        read_value!($iter, String).bytes().collect::<Vec<u8>>()
    };
    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };
    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}

//

type DP = [f64; 2];

fn factor(s: &[u8], k: &mut usize, p: &[f64]) -> DP {
    let c = s[*k];
    match c {
        b'Y' => {
            *k += 3;
            [0.0, 1.0]
        },
        b'N' => {
            *k += 2;
            [1.0, 0.0]
        },
        b'r' => {
            *k += 7;
            let a = exp(s, k, p);
            let b = exp(s, k, p);
            *k += 1;
            let x = a[1] * b[1] * p[0] + (1.0 - a[1] * b[1]) * p[1];
            [1.0 - x, x]
        },
        b'(' => {
            *k += 1;
            let res = exp(s, k, p);
            *k += 1;
            res
        },
        _ => unreachable!(),
    }
}

fn term(s: &[u8], k: &mut usize, p: &[f64]) -> DP {
    let mut res = factor(s, k, p);
    while *k < s.len() && s[*k] == b'a' {
        *k += 3;
        let v = term(s, k, p);
        let x = res[1] * v[1];
        let x = x * (1.0 - p[2]) + (1.0 - x) * p[2];
        res = [1.0 - x, x];
    }
    res
}

fn exp(s: &[u8], k: &mut usize, p: &[f64]) -> DP {
    let mut res = term(s, k, p);
    while *k < s.len() && s[*k] == b'o' {
        *k += 2;
        let v = term(s, k, p);
        let x = 1.0 - res[0] * v[0];
        let x = x * (1.0 - p[2]) + (1.0 - x) * p[2];
        res = [1.0 - x, x];
    }
    res
}

fn run() {
    input! {
        n: usize,
        p: [f64; 3],
        s: bytes,
    }
    let ans = exp(&s, &mut 0, &p)[1];
    println!("{}", (ans * 100.0 + 0.1).floor() as usize);
}

fn main() {
    run();
}
0