結果

問題 No.150 "良問"(良問とは言っていない
ユーザー tentententen
提出日時 2022-10-13 11:28:52
言語 Java21
(openjdk 21)
結果
AC  
実行時間 56 ms / 5,000 ms
コード長 2,425 bytes
コンパイル時間 2,243 ms
コンパイル使用メモリ 74,448 KB
実行使用メモリ 50,196 KB
最終ジャッジ日時 2023-09-08 19:01:22
合計ジャッジ時間 4,779 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 48 ms
49,324 KB
testcase_01 AC 44 ms
49,336 KB
testcase_02 AC 44 ms
49,212 KB
testcase_03 AC 45 ms
49,208 KB
testcase_04 AC 45 ms
49,228 KB
testcase_05 AC 45 ms
49,492 KB
testcase_06 AC 56 ms
50,196 KB
testcase_07 AC 44 ms
49,032 KB
testcase_08 AC 44 ms
49,020 KB
testcase_09 AC 44 ms
49,500 KB
testcase_10 AC 50 ms
47,388 KB
testcase_11 AC 48 ms
49,396 KB
testcase_12 AC 48 ms
49,256 KB
testcase_13 AC 48 ms
49,068 KB
testcase_14 AC 51 ms
49,064 KB
testcase_15 AC 49 ms
49,076 KB
testcase_16 AC 45 ms
49,016 KB
testcase_17 AC 46 ms
49,368 KB
testcase_18 AC 50 ms
49,136 KB
testcase_19 AC 52 ms
49,784 KB
testcase_20 AC 52 ms
49,256 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        char[] GOOD = "good".toCharArray();
        char[] PROBLEM = "problem".toCharArray();
        int n = sc.nextInt();
        StringBuilder sb = new StringBuilder();
        while (n-- > 0) {
            char[] values = sc.next().toCharArray();
            int[] goods = getDPLeft(GOOD, values);
            int[] problems = getDPRight(PROBLEM, values);
            int min = Integer.MAX_VALUE;
            for (int i = 0; i + 4 < values.length; i++) {
                min = Math.min(min, goods[i] + problems[i + 4]);
            }
            sb.append(min).append("\n");
        }
        System.out.print(sb);
    }
    
    static int[] getDPLeft(char[] target, char[] base) {
        int[] dp = getDP(target, base);
        for (int i = 1; i < dp.length; i++) {
            dp[i] = Math.min(dp[i], dp[i - 1]);
        }
        return dp;
    }
    
    static int[] getDPRight(char[] target, char[] base) {
        int[] dp = getDP(target, base);
        for (int i = dp.length - 1; i > 0; i--) {
            dp[i - 1] = Math.min(dp[i - 1], dp[i]);
        }
        return dp;
    }
    
    static int[] getDP(char[] target, char[] base) {
        int[] dp = new int[base.length];
        Arrays.fill(dp, Integer.MAX_VALUE / 2);
        for (int i = 0; i + target.length <= base.length; i++) {
            dp[i] = 0;
            for (int j = 0; j < target.length; j++) {
                if (target[j] != base[i + j]) {
                    dp[i]++;
                }
            }
        }
        return dp;
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0