結果

問題 No.1542 ぽんぽんぽん ぽんぽんぽんぽん ぽんぽんぽん
ユーザー tentententen
提出日時 2021-06-07 16:48:29
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,994 bytes
コンパイル時間 2,408 ms
コンパイル使用メモリ 74,188 KB
実行使用メモリ 53,740 KB
最終ジャッジ日時 2023-08-16 09:52:03
合計ジャッジ時間 9,409 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
49,256 KB
testcase_01 AC 42 ms
49,296 KB
testcase_02 AC 42 ms
49,184 KB
testcase_03 AC 43 ms
49,644 KB
testcase_04 AC 68 ms
51,124 KB
testcase_05 WA -
testcase_06 AC 136 ms
53,188 KB
testcase_07 AC 68 ms
51,028 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 56 ms
50,892 KB
testcase_13 AC 88 ms
52,504 KB
testcase_14 AC 160 ms
52,588 KB
testcase_15 AC 73 ms
51,928 KB
testcase_16 WA -
testcase_17 AC 43 ms
49,204 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 43 ms
49,340 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 244 ms
52,540 KB
testcase_25 AC 241 ms
53,352 KB
testcase_26 AC 247 ms
52,392 KB
testcase_27 AC 248 ms
52,976 KB
testcase_28 AC 247 ms
52,264 KB
testcase_29 AC 263 ms
52,972 KB
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static int n;
    static char[] pons;
    static int[][] dp;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        n = sc.nextInt();
        pons = sc.next().toCharArray();
        dp = new int[n][n];
        for (int i = 0; i < n; i++) {
            for (int j = i + 2; j < n; j++) {
                dp[i][j] = -1;
            }
        }
        int max = -1;
        for (int i = 2; i < n - 3; i++) {
            int left = dfw(0, i);
            int right = dfw(i + 1, n - 1);
            if (left > 0 && right > 0) {
                max = Math.max(max, left + right - 2);
            }
        }
        System.out.println(max);
    }
    
    static int dfw(int start, int end) {
        if (dp[start][end] < 0) {
            int max = 0;
            for (int i = start + 1; i < end; i++) {
                max = Math.max(max, dfw(start, i) + dfw(i + 1, end));
            }
            if (pons[start] == 'p' && pons[end] == 'n') {
                for (int i = start + 1; i < end; i++) {
                    if (pons[i] != 'o') {
                        continue;
                    }
                    max = Math.max(max, dfw(start + 1, i - 1) + dfw(i + 1, end - 1) + 1);
                }
            }
            dp[start][end] = max;
        }
        return dp[start][end];
    }
}
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