結果

問題 No.1646 Avoid Palindrome
ユーザー 👑 terry_u16terry_u16
提出日時 2021-08-13 23:13:10
言語 Rust
(1.77.0)
結果
TLE  
実行時間 -
コード長 3,065 bytes
コンパイル時間 1,261 ms
コンパイル使用メモリ 175,128 KB
実行使用メモリ 314,752 KB
最終ジャッジ日時 2024-04-14 20:21:28
合計ジャッジ時間 33,708 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
13,760 KB
testcase_01 AC 1 ms
6,816 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 3 ms
6,944 KB
testcase_04 AC 2,847 ms
311,680 KB
testcase_05 AC 2,828 ms
311,680 KB
testcase_06 AC 2,724 ms
301,056 KB
testcase_07 AC 2,827 ms
311,680 KB
testcase_08 AC 2,835 ms
314,752 KB
testcase_09 AC 2,686 ms
299,008 KB
testcase_10 AC 2,746 ms
306,432 KB
testcase_11 AC 2,682 ms
297,984 KB
testcase_12 AC 2,832 ms
311,040 KB
testcase_13 AC 2,843 ms
314,624 KB
testcase_14 TLE -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
権限があれば一括ダウンロードができます

ソースコード

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];
                    dp[i + 1][l1][c] %= 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