結果

問題 No.1646 Avoid Palindrome
ユーザー phspls
提出日時 2022-10-17 11:38:06
言語 Rust
(1.83.0 + proconio)
結果
WA  
実行時間 -
コード長 1,539 bytes
コンパイル時間 19,570 ms
コンパイル使用メモリ 377,856 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-06-28 02:23:50
合計ジャッジ時間 22,118 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 9 WA * 31
権限があれば一括ダウンロードができます

ソースコード

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[pprev][prev];
                    next_dp[cur][prev] %= MOD;
                }
            }
        }
        dp = next_dp;
    }

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