結果

問題 No.1646 Avoid Palindrome
ユーザー 👑 terry_u16terry_u16
提出日時 2021-08-13 23:15:22
言語 Rust
(1.77.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 3,148 bytes
コンパイル時間 918 ms
コンパイル使用メモリ 177,812 KB
実行使用メモリ 330,752 KB
最終ジャッジ日時 2024-04-14 20:27:58
合計ジャッジ時間 73,154 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 1,686 ms
311,552 KB
testcase_05 AC 1,695 ms
311,552 KB
testcase_06 AC 1,634 ms
301,056 KB
testcase_07 AC 1,701 ms
311,680 KB
testcase_08 AC 1,688 ms
314,496 KB
testcase_09 AC 1,612 ms
299,008 KB
testcase_10 AC 1,655 ms
306,432 KB
testcase_11 AC 1,612 ms
297,984 KB
testcase_12 AC 1,690 ms
311,168 KB
testcase_13 AC 1,705 ms
314,496 KB
testcase_14 AC 2,916 ms
305,664 KB
testcase_15 AC 2,976 ms
311,680 KB
testcase_16 AC 2,892 ms
303,744 KB
testcase_17 TLE -
testcase_18 AC 2,953 ms
308,608 KB
testcase_19 AC 2,937 ms
307,200 KB
testcase_20 AC 2,969 ms
309,504 KB
testcase_21 TLE -
testcase_22 AC 2,918 ms
305,792 KB
testcase_23 TLE -
testcase_24 TLE -
testcase_25 TLE -
testcase_26 TLE -
testcase_27 TLE -
testcase_28 TLE -
testcase_29 AC 876 ms
330,624 KB
testcase_30 AC 877 ms
330,624 KB
testcase_31 AC 867 ms
330,624 KB
testcase_32 AC 875 ms
330,624 KB
testcase_33 AC 874 ms
330,496 KB
testcase_34 TLE -
testcase_35 AC 1 ms
6,944 KB
testcase_36 AC 1 ms
6,944 KB
testcase_37 AC 1 ms
6,944 KB
testcase_38 AC 1 ms
6,940 KB
testcase_39 AC 1 ms
6,944 KB
testcase_40 AC 1 ms
6,940 KB
testcase_41 AC 1 ms
6,944 KB
testcase_42 AC 1 ms
6,944 KB
testcase_43 AC 878 ms
330,624 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

pub trait ChangeBest {
    fn chmin(&mut self, v: Self) -> bool;
    fn chmax(&mut self, v: Self) -> bool;
}

impl<T> ChangeBest for T
where
    T: PartialOrd,
{
    fn chmin(&mut self, v: T) -> bool {
        if *self > v {
            *self = v;
            true
        } else {
            false
        }
    }

    fn chmax(&mut self, v: T) -> bool {
        if *self < v {
            *self = v;
            true
        } else {
            false
        }
    }
}

#[allow(unused_macros)]
macro_rules! mat {
    ($e:expr; $d:expr) => { vec![$e; $d] };
    ($e:expr; $d:expr $(; $ds:expr)+) => { vec![mat![$e $(; $ds)*]; $d] };
}

#[allow(unused_macros)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

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

#[allow(unused_macros)]
macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

const MOD: u64 = 998244353;

fn main() {
    input! {
        n: usize,
        s: String,
    };

    let s = s.chars().collect::<Vec<_>>();

    let mut dp = mat![0; s.len() + 1; 27; 27];
    dp[0][26][26] = 1;

    for i in 0..s.len() {
        let iter = if s[i] == '?' {
            0..26
        } else {
            let c = s[i] as usize - 'a' as usize;
            c..(c + 1)
        };

        for c in iter {
            for l1 in 0..27 {
                if c == l1 {
                    continue;
                }

                for l2 in 0..27 {
                    if c == l2 {
                        continue;
                    }

                    dp[i + 1][l1][c] += dp[i][l2][l1];
                }
            }
        }

        for d in dp[i + 1].iter_mut() {
            for d in d.iter_mut() {
                *d %= MOD;
            }
        }
    }

    let mut sum = 0;

    for l1 in 0..27 {
        for l2 in 0..27 {
            sum += dp[n][l2][l1];
            sum %= MOD;
        }
    }

    println!("{}", sum);
}
0