結果

問題 No.252 "良問"(良問とは言っていない (2)
ユーザー GrenacheGrenache
提出日時 2015-07-27 15:28:49
言語 Java21
(openjdk 21)
結果
AC  
実行時間 560 ms / 2,000 ms
コード長 2,645 bytes
コンパイル時間 3,707 ms
コンパイル使用メモリ 75,608 KB
実行使用メモリ 78,972 KB
最終ジャッジ日時 2023-09-23 03:57:46
合計ジャッジ時間 7,012 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 325 ms
57,088 KB
testcase_01 AC 560 ms
69,888 KB
testcase_02 AC 307 ms
65,832 KB
testcase_03 AC 270 ms
78,972 KB
testcase_04 AC 270 ms
78,752 KB
testcase_05 AC 258 ms
78,932 KB
testcase_06 AC 46 ms
49,180 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Iterator;


public class Main_yukicoder252 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int t = sc.nextInt();
        for (int i = 0; i < t; i++) {
        	char[] s = sc.next().toCharArray();
        	int n = s.length;
        	
        	int[] g = new int[n];
        	for (int j = 0; j + 3 < n; j++) {
        		g[j] = ham(s, j, "good");
        		if (j > 0) {
        			g[j] = Math.min(g[j - 1], g[j]);
        		}
        	}
        	int[] p = new int[n];
        	int[] p0 = new int[n];
        	for (int j = 0; j + 6 < n; j++) {
        		p[j] = ham(s, j, "problem");
        		if (j > 0) {
        			if (p[j] == 0) {
        				p0[j] = p0[j - 1] + 1;
        			} else {
        				p0[j] = p0[j - 1];
        			}
        		} else {
        			if (p[j] == 0) {
        				p0[j] = 1;
        			} else {
        				p0[j] = 0;
        			}
        		}
        	}
        	for (int j = n - 7; j >= 0; j--) {
        		if (j < n - 7) {
        			p[j] = Math.min(p[j + 1], p[j]);
        		}
        	}
        	
        	int min = Integer.MAX_VALUE;
        	for (int j = 4; j + 6 < n; j++) {
        		min = Math.min(min, g[j - 4] + p[j] + (j >= 10 ? p0[j - 10] : 0));
        	}
        	
        	System.out.println(min);
        }

        sc.close();
    }

	private static int ham(char[] s, int j, String string) {
		char[] tmp = string.toCharArray();
		
		int ret = 0;
		for (int i = 0; i < tmp.length; i++) {
			if (s[j + i] != tmp[i]) {
				ret++;
			}
		}

		return ret;
	}

	@SuppressWarnings("unused")
	private static class Scanner {
		BufferedReader br;
		Iterator<String> it;
		
		Scanner (InputStream in) {
			br = new BufferedReader(new InputStreamReader(in));
		}
		
		String next() throws RuntimeException  {
			try {
				if (it == null || !it.hasNext()) {
					it = Arrays.asList(br.readLine().split(" ")).iterator();
				}
				return it.next();
			} catch (IOException e) {
				throw new IllegalStateException();
			}
		}
		
		int nextInt() throws RuntimeException {
			return Integer.parseInt(next());
		}

		long nextLong() throws RuntimeException {
			return Long.parseLong(next());
		}

		float nextFloat() throws RuntimeException {
			return Float.parseFloat(next());
		}

		double nextDouble() throws RuntimeException {
			return Double.parseDouble(next());
		}

		void close() {
			try {
				br.close();
			} catch (IOException e) {
//				throw new IllegalStateException();
			}
		}
	}
}
0