結果

問題 No.2926 Botaoshi
ユーザー tenten
提出日時 2024-10-21 11:05:42
言語 Java
(openjdk 23)
結果
AC  
実行時間 194 ms / 2,000 ms
コード長 2,281 bytes
コンパイル時間 2,782 ms
コンパイル使用メモリ 78,460 KB
実行使用メモリ 80,924 KB
最終ジャッジ日時 2024-10-21 11:05:53
合計ジャッジ時間 10,644 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 42
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;
import java.util.function.*;
import java.util.stream.*;

public class Main {
    static char[] status;
    static int[][] dp;
    static final int MOD = 998244353;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        status = sc.next().toCharArray();
        dp = new int[n][2];
        for (int[] arr : dp) {
            Arrays.fill(arr, -1);
        }
        System.out.println(dfw(n - 1, 0));
    }
    
    static int dfw(int idx, int v) {
        if (idx < 0) {
            return 1;
        }
        if (dp[idx][v] < 0) {
            if (v == 0) {
                if (status[idx] == '.') {
                    dp[idx][v] = (dfw(idx - 1, 1) + dfw(idx - 1, 0) * 2 % MOD) % MOD;
                } else if (status[idx] == 'L') {
                    dp[idx][v] = dfw(idx - 1, 1);
                } else {
                    dp[idx][v] = dfw(idx - 1, 0);
                }
            } else {
                if (status[idx] == '.') {
                    dp[idx][v] = (dfw(idx - 1, 1) + dfw(idx - 1, 0)) % MOD;
                } else if (status[idx] == 'L') {
                    dp[idx][v] = dfw(idx - 1, 1);
                } else if (status[idx] == 'R') {
                    dp[idx][v] = 0;
                } else {
                    dp[idx][v] = dfw(idx - 1, 0);
                }
            }
        }
        return dp[idx][v];
    }
}

class Scanner {
    BufferedReader br;
    StringTokenizer st = new StringTokenizer("");

    public Scanner() {
        try {
            br = new BufferedReader(new InputStreamReader(System.in));
        } catch (Exception e) {
            
        }
    }
    
    public int nextInt() {
        return Integer.parseInt(next());
    }
    
    public long nextLong() {
        return Long.parseLong(next());
    }
    
    public double nextDouble() {
        return Double.parseDouble(next());
    }
    
    public String next() {
        try {
            while (!st.hasMoreTokens()) {
                st = new StringTokenizer(br.readLine());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return st.nextToken();
        }
    }
    
}


0