結果

問題 No.1542 ぽんぽんぽん ぽんぽんぽんぽん ぽんぽんぽん
ユーザー tenten
提出日時 2021-06-07 16:48:29
言語 Java
(openjdk 23)
結果
WA  
実行時間 -
コード長 1,994 bytes
コンパイル時間 2,696 ms
コンパイル使用メモリ 77,316 KB
実行使用メモリ 55,228 KB
最終ジャッジ日時 2024-11-24 22:04:42
合計ジャッジ時間 9,423 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 16 WA * 14
権限があれば一括ダウンロードができます

ソースコード

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