結果

問題 No.1677 mæx
ユーザー merlinmerlin
提出日時 2021-09-10 23:27:53
言語 Java21
(openjdk 21)
結果
AC  
実行時間 202 ms / 2,000 ms
コード長 2,221 bytes
コンパイル時間 2,378 ms
コンパイル使用メモリ 75,208 KB
実行使用メモリ 57,844 KB
最終ジャッジ日時 2023-09-02 22:13:04
合計ジャッジ時間 7,030 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
47,660 KB
testcase_01 AC 42 ms
49,412 KB
testcase_02 AC 43 ms
49,492 KB
testcase_03 AC 42 ms
49,304 KB
testcase_04 AC 188 ms
56,644 KB
testcase_05 AC 188 ms
57,060 KB
testcase_06 AC 191 ms
57,216 KB
testcase_07 AC 187 ms
56,672 KB
testcase_08 AC 188 ms
56,896 KB
testcase_09 AC 190 ms
57,192 KB
testcase_10 AC 189 ms
56,740 KB
testcase_11 AC 187 ms
56,456 KB
testcase_12 AC 184 ms
56,808 KB
testcase_13 AC 187 ms
56,852 KB
testcase_14 AC 201 ms
57,844 KB
testcase_15 AC 200 ms
57,160 KB
testcase_16 AC 196 ms
57,056 KB
testcase_17 AC 196 ms
57,360 KB
testcase_18 AC 196 ms
57,140 KB
testcase_19 AC 42 ms
49,636 KB
testcase_20 AC 45 ms
49,384 KB
testcase_21 AC 202 ms
57,160 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

class Main
{
    public static void main(String args[])throws Exception
    {
        BufferedReader bu=new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb=new StringBuilder();
        String s=bu.readLine();
        int n=s.length();
        Stack<Integer> st=new Stack<>();
        Stack<long[]> val=new Stack<>();
        //0-( 1-max 2-mex 3-m?x 4-0,1,2,?
        int i; long M=998244353;
        for(i=0;i<n;i++)
        {
            char ch=s.charAt(i);
            if(ch==',') continue;

            if(ch=='(') {}//do nothing
            else if(ch!=')')
            {
                int add=-1; long adr[]=new long[3];
                if(ch=='m')
                {
                    if(s.charAt(i+1)=='?') add=3;
                    else if(s.charAt(i+1)=='e') add=2;
                    else add=1;
                    i+=2;
                }
                else
                {
                    add=4;
                    if(ch=='?') adr[0]=adr[1]=adr[2]=1;
                    else adr[ch-'0']=1;
                }
                st.add(add);
                val.add(adr);
            }

            if(ch!=')') continue;
            long c1[]=val.pop(),c2[]=val.pop(),c3[]=val.pop();
            st.pop(); st.pop(); int op=st.pop();

            int j,k,mex; boolean v[]=new boolean[3];
            for(j=0;j<3;j++)
            for(k=0;k<3;k++)
            if(op==1) c3[Math.max(j,k)]=(c3[Math.max(j,k)]+c1[j]*c2[k]%M)%M;
            else if(op==2)
            {
                v[0]=v[1]=v[2]=false;
                v[j]=v[k]=true;
                mex=0;
                while(mex<3 && v[mex]) mex++;
                c3[mex]=(c3[mex]+(c1[j]*c2[k])%M)%M;
            }
            else
            {
                c3[Math.max(j,k)]=(c3[Math.max(j,k)]+c1[j]*c2[k]%M)%M;
                v[0]=v[1]=v[2]=false;
                v[j]=v[k]=true;
                mex=0;
                while(mex<3 && v[mex]) mex++;
                c3[mex]=(c3[mex]+(c1[j]*c2[k])%M)%M;
            }
            val.add(c3);
            st.add(4);
        }

        int k=Integer.parseInt(bu.readLine());
        System.out.println(val.pop()[k]);
    }
}
0