結果

問題 No.1677 mæx
ユーザー tenten
提出日時 2021-09-14 10:57:52
言語 Java
(openjdk 23)
結果
AC  
実行時間 143 ms / 2,000 ms
コード長 3,563 bytes
コンパイル時間 2,328 ms
コンパイル使用メモリ 77,692 KB
実行使用メモリ 45,372 KB
最終ジャッジ日時 2024-06-27 00:08:51
合計ジャッジ時間 5,776 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 18
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;

public class Main {
    static int idx = 0;
    static final int MOD = 998244353;
    static int[][] def = new int[][]{{1, 0, 0}, {0, 1, 0}, {0, 0, 1}, {1, 1, 1}};
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int[] dp = search(sc.next().toCharArray());
        System.out.println(dp[sc.nextInt()]);
    }
    
    static int[] search(char[] arr) {
        if (arr[idx] == 'm') {
            if (arr[idx + 1] == '?') {
                idx += 4;
                return mxx(arr);
            } else if (arr[idx + 1] == 'a') {
                idx += 4;
                return max(arr);
            } else {
                idx += 4;
                return mex(arr);
            }
        } else if (arr[idx] == '?') {
            idx++;
            return def[3];
        } else {
            idx++;
            return def[arr[idx - 1] - '0'];
        }
    }
    
    static int[] max(char[] arr) {
        int[] dp1 = search(arr);
        idx++;
        int[] dp2 = search(arr);
        idx++;
        return max(dp1, dp2);
    }
    
    static int[] max(int[] dp1, int[] dp2) {
        int[] ans = new int[3];
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                ans[Math.max(i, j)] += calc(dp1[i], dp2[j]);
                ans[Math.max(i, j)] %= MOD;
            }
        }
        return ans;
    }
    
    static int[] mex(char[] arr) {
        int[] dp1 = search(arr);
        idx++;
        int[] dp2 = search(arr);
        idx++;
        return mex(dp1, dp2);
    }
    
    
    static int[] mex(int[] dp1, int[] dp2) {
        int[] ans = new int[3];
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                ans[mex(i, j)] += calc(dp1[i], dp2[j]);
                ans[mex(i, j)] %= MOD;
            }
        }
        return ans;
    }
    
    static int[] mxx(char[] arr) {
        int[] dp1 = search(arr);
        idx++;
        int[] dp2 = search(arr);
        idx++;
        return mxx(dp1, dp2);
    }
    
    static int[] mxx(int[] dp1, int[] dp2) {
        int[] ans = new int[3];
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                ans[Math.max(i, j)] += calc(dp1[i], dp2[j]);
                ans[Math.max(i, j)] %= MOD;
                ans[mex(i, j)] += calc(dp1[i], dp2[j]);
                ans[mex(i, j)] %= MOD;
            }
        }
        return ans;
    }
    
    static int calc(int x, int y) {
        return (int)((long)x * y % MOD);
    }
    
    
    static int mex (int x, int y) {
        if (x == 0) {
            if (y == 1) {
                return 2;
            } else {
                return 1;
            }
        } else if (y == 0) {
            if (x == 1) {
                return 2;
            } else {
                return 1;
            }
        } else {
            return 0;
        }
    }
}

class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public String next() throws Exception {
        if (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0