結果

問題 No.1677 mæx
ユーザー tentententen
提出日時 2021-09-14 10:57:52
言語 Java21
(openjdk 21)
結果
AC  
実行時間 140 ms / 2,000 ms
コード長 3,563 bytes
コンパイル時間 2,139 ms
コンパイル使用メモリ 74,688 KB
実行使用メモリ 59,340 KB
最終ジャッジ日時 2023-09-09 07:00:02
合計ジャッジ時間 6,033 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
49,528 KB
testcase_01 AC 43 ms
49,392 KB
testcase_02 AC 43 ms
49,440 KB
testcase_03 AC 43 ms
49,844 KB
testcase_04 AC 132 ms
58,060 KB
testcase_05 AC 125 ms
58,628 KB
testcase_06 AC 126 ms
58,720 KB
testcase_07 AC 125 ms
58,940 KB
testcase_08 AC 126 ms
57,840 KB
testcase_09 AC 126 ms
57,596 KB
testcase_10 AC 132 ms
57,800 KB
testcase_11 AC 128 ms
58,352 KB
testcase_12 AC 130 ms
57,328 KB
testcase_13 AC 132 ms
58,856 KB
testcase_14 AC 132 ms
58,444 KB
testcase_15 AC 124 ms
58,792 KB
testcase_16 AC 134 ms
57,816 KB
testcase_17 AC 134 ms
59,340 KB
testcase_18 AC 134 ms
58,520 KB
testcase_19 AC 44 ms
49,396 KB
testcase_20 AC 43 ms
49,412 KB
testcase_21 AC 140 ms
58,684 KB
権限があれば一括ダウンロードができます

ソースコード

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