結果

問題 No.150 "良問"(良問とは言っていない
ユーザー jp_stejp_ste
提出日時 2015-03-29 05:28:55
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,872 bytes
コンパイル時間 2,466 ms
コンパイル使用メモリ 73,964 KB
実行使用メモリ 59,584 KB
最終ジャッジ日時 2023-09-16 23:58:54
合計ジャッジ時間 7,036 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 109 ms
55,700 KB
testcase_02 AC 113 ms
56,120 KB
testcase_03 AC 110 ms
55,728 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 108 ms
55,748 KB
testcase_08 WA -
testcase_09 AC 110 ms
55,836 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

package No150;

import java.util.Scanner;

public class Main {
	
	static final String GOOD_STRING = "good";
	static final String PROBLEM_STRING = "problem";
	
	static int mMinLengthOfGood = Integer.MAX_VALUE;
	static int mMinLengthOfProblem = Integer.MAX_VALUE;
	static int mAns = Integer.MAX_VALUE;
	
	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		for(int i=0; i<N; i++) {
			
			mMinLengthOfGood = Integer.MAX_VALUE;
			mMinLengthOfProblem = Integer.MAX_VALUE;
			mAns = Integer.MAX_VALUE;
			
			String T = sc.next();
			serchGood(T);
			System.out.println(mAns);
		}
	}
	
	private static void serchGood(String str) {
		
		String base = GOOD_STRING;
		
		for(int i=0; i<=str.length()-base.length(); i++) {
			
			String target = str.substring(i, base.length()+i);
			int num = unmatchCharacterNum(target, base);
			
			mMinLengthOfGood = Math.min(mMinLengthOfGood, num);
			
			String nextStr = str.substring( base.length()+i);
			serchProblem( nextStr );
		}
	}
	
	private static void serchProblem(String str) {
		
		String base = PROBLEM_STRING;
		
		if(str.length() < base.length() ) return;
					
		for(int i=0; i<=str.length()-base.length(); i++) {
			
			String target = str.substring(i, base.length()+i);
			int num = unmatchCharacterNum(target, base);
			
			mMinLengthOfProblem = Math.min(mMinLengthOfProblem, num);
			
			mAns = Math.min(mAns, mMinLengthOfGood + mMinLengthOfProblem);
		}
	}
	
	//一致していない文字の個数を返す。
	private static int unmatchCharacterNum(String target, String base) {
		if(target.equals(base)) {
			return 0;
		}
		if(target.length() != base.length()) {
			throw new IllegalArgumentException("length is bad");
		}
		int count = 0;
		for(int i=0; i<target.length(); i++) {
			if(target.charAt(i) != base.charAt(i)) count++;
		}
		return count;
	}
}
0