結果

問題 No.1646 Avoid Palindrome
ユーザー phsplsphspls
提出日時 2022-10-17 11:45:50
言語 Rust
(1.77.0)
結果
AC  
実行時間 267 ms / 3,000 ms
コード長 1,539 bytes
コンパイル時間 793 ms
コンパイル使用メモリ 147,548 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-10 11:05:25
合計ジャッジ時間 10,431 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,384 KB
testcase_04 AC 217 ms
4,376 KB
testcase_05 AC 216 ms
4,380 KB
testcase_06 AC 209 ms
4,376 KB
testcase_07 AC 216 ms
4,376 KB
testcase_08 AC 219 ms
4,376 KB
testcase_09 AC 208 ms
4,380 KB
testcase_10 AC 215 ms
4,376 KB
testcase_11 AC 206 ms
4,376 KB
testcase_12 AC 217 ms
4,380 KB
testcase_13 AC 219 ms
4,376 KB
testcase_14 AC 243 ms
4,376 KB
testcase_15 AC 250 ms
4,380 KB
testcase_16 AC 245 ms
4,376 KB
testcase_17 AC 258 ms
4,380 KB
testcase_18 AC 251 ms
4,376 KB
testcase_19 AC 246 ms
4,376 KB
testcase_20 AC 252 ms
4,376 KB
testcase_21 AC 254 ms
4,380 KB
testcase_22 AC 245 ms
4,376 KB
testcase_23 AC 257 ms
4,380 KB
testcase_24 AC 267 ms
4,376 KB
testcase_25 AC 266 ms
4,376 KB
testcase_26 AC 264 ms
4,380 KB
testcase_27 AC 266 ms
4,376 KB
testcase_28 AC 266 ms
4,380 KB
testcase_29 AC 202 ms
4,380 KB
testcase_30 AC 203 ms
4,380 KB
testcase_31 AC 202 ms
4,376 KB
testcase_32 AC 202 ms
4,376 KB
testcase_33 AC 201 ms
4,380 KB
testcase_34 AC 267 ms
4,380 KB
testcase_35 AC 1 ms
4,376 KB
testcase_36 AC 1 ms
4,376 KB
testcase_37 AC 1 ms
4,380 KB
testcase_38 AC 1 ms
4,380 KB
testcase_39 AC 1 ms
4,380 KB
testcase_40 AC 1 ms
4,376 KB
testcase_41 AC 1 ms
4,376 KB
testcase_42 AC 1 ms
4,380 KB
testcase_43 AC 201 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

const MOD: usize = 998244353;
const SIZE: usize = 26;

fn main() {
    let mut n = String::new();
    std::io::stdin().read_line(&mut n).ok();
    let n: usize = n.trim().parse().unwrap();
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    let s = s.trim().chars().collect::<Vec<char>>();

    let mut dp = vec![vec![0usize; SIZE+1]; SIZE+1];
    dp[SIZE][SIZE] = 1;
    for i in 0..n {
        let mut next_dp = vec![vec![0usize; SIZE+1]; SIZE+1];
        if s[i] == '?' {
            let mut summary = vec![0usize; SIZE+1];
            for pprev in 0..=SIZE {
                for prev in 0..=SIZE {
                    summary[prev] += dp[prev][pprev];
                    summary[prev] %= MOD;
                }
            }
            for prev in 0..=SIZE {
                for cur in 0..SIZE {
                    if prev == cur { continue; }
                    next_dp[cur][prev] = MOD + summary[prev] - dp[prev][cur];
                    next_dp[cur][prev] %= MOD;
                }
            }
        } else {
            let cur = s[i] as usize - 'a' as usize;
            for pprev in 0..=SIZE {
                if cur == pprev { continue; }
                for prev in 0..=SIZE {
                    if cur == prev { continue; }
                    next_dp[cur][prev] += dp[prev][pprev];
                    next_dp[cur][prev] %= MOD;
                }
            }
        }
        dp = next_dp;
    }

    println!("{}", dp.iter().map(|v| v.iter().sum::<usize>()).sum::<usize>() % MOD);
}
0