結果

問題 No.1646 Avoid Palindrome
ユーザー phsplsphspls
提出日時 2022-10-05 00:15:33
言語 Rust
(1.77.0)
結果
AC  
実行時間 258 ms / 3,000 ms
コード長 1,535 bytes
コンパイル時間 1,782 ms
コンパイル使用メモリ 146,288 KB
実行使用メモリ 4,500 KB
最終ジャッジ日時 2023-08-28 23:57:03
合計ジャッジ時間 11,658 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 239 ms
4,376 KB
testcase_05 AC 239 ms
4,376 KB
testcase_06 AC 231 ms
4,380 KB
testcase_07 AC 238 ms
4,380 KB
testcase_08 AC 241 ms
4,380 KB
testcase_09 AC 230 ms
4,376 KB
testcase_10 AC 236 ms
4,376 KB
testcase_11 AC 228 ms
4,380 KB
testcase_12 AC 239 ms
4,376 KB
testcase_13 AC 241 ms
4,376 KB
testcase_14 AC 237 ms
4,376 KB
testcase_15 AC 244 ms
4,376 KB
testcase_16 AC 238 ms
4,380 KB
testcase_17 AC 251 ms
4,376 KB
testcase_18 AC 242 ms
4,380 KB
testcase_19 AC 238 ms
4,380 KB
testcase_20 AC 246 ms
4,376 KB
testcase_21 AC 245 ms
4,376 KB
testcase_22 AC 237 ms
4,376 KB
testcase_23 AC 250 ms
4,380 KB
testcase_24 AC 258 ms
4,380 KB
testcase_25 AC 257 ms
4,376 KB
testcase_26 AC 256 ms
4,380 KB
testcase_27 AC 257 ms
4,376 KB
testcase_28 AC 256 ms
4,380 KB
testcase_29 AC 248 ms
4,376 KB
testcase_30 AC 249 ms
4,376 KB
testcase_31 AC 248 ms
4,376 KB
testcase_32 AC 248 ms
4,376 KB
testcase_33 AC 248 ms
4,376 KB
testcase_34 AC 257 ms
4,376 KB
testcase_35 AC 1 ms
4,380 KB
testcase_36 AC 1 ms
4,380 KB
testcase_37 AC 1 ms
4,500 KB
testcase_38 AC 1 ms
4,376 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 248 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<_>>();

    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[pprev][prev];
                    summary[prev] %= MOD;
                }
            }
            for cur in 0..SIZE {
                for prev in 0..=SIZE {
                    if prev == cur { continue; }
                    next_dp[prev][cur] = MOD + summary[prev] - dp[cur][prev];
                    next_dp[prev][cur] %= MOD;
                }
            }
        } else {
            let idx = s[i] as usize - 'a' as usize;
            for prev in 0..=SIZE {
                if prev == idx { continue; }
                for pprev in 0..=SIZE {
                    if pprev == idx { continue; }
                    next_dp[prev][idx] += dp[pprev][prev];
                    next_dp[prev][idx] %= MOD;
                }
            }
        }
        dp = next_dp;
    }
    println!("{}", dp.iter().map(|v| v.iter().sum::<usize>()).sum::<usize>() % MOD);
}
0