結果

問題 No.345 最小チワワ問題
ユーザー nihi9119nihi9119
提出日時 2017-06-04 22:43:44
言語 Java21
(openjdk 21)
結果
AC  
実行時間 59 ms / 2,000 ms
コード長 3,105 bytes
コンパイル時間 4,347 ms
コンパイル使用メモリ 79,016 KB
実行使用メモリ 53,308 KB
最終ジャッジ日時 2023-10-26 04:10:40
合計ジャッジ時間 7,410 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 56 ms
53,288 KB
testcase_01 AC 56 ms
53,276 KB
testcase_02 AC 58 ms
53,292 KB
testcase_03 AC 56 ms
52,384 KB
testcase_04 AC 56 ms
52,384 KB
testcase_05 AC 57 ms
53,296 KB
testcase_06 AC 56 ms
53,288 KB
testcase_07 AC 57 ms
53,308 KB
testcase_08 AC 55 ms
53,288 KB
testcase_09 AC 55 ms
53,288 KB
testcase_10 AC 56 ms
53,284 KB
testcase_11 AC 58 ms
53,300 KB
testcase_12 AC 59 ms
53,284 KB
testcase_13 AC 55 ms
53,288 KB
testcase_14 AC 58 ms
50,364 KB
testcase_15 AC 56 ms
53,288 KB
testcase_16 AC 56 ms
53,292 KB
testcase_17 AC 56 ms
53,228 KB
testcase_18 AC 55 ms
53,288 KB
testcase_19 AC 55 ms
52,420 KB
testcase_20 AC 56 ms
52,380 KB
testcase_21 AC 56 ms
52,400 KB
testcase_22 AC 55 ms
53,280 KB
testcase_23 AC 55 ms
53,288 KB
testcase_24 AC 56 ms
53,284 KB
testcase_25 AC 55 ms
53,292 KB
testcase_26 AC 56 ms
53,276 KB
testcase_27 AC 54 ms
52,380 KB
testcase_28 AC 56 ms
52,404 KB
testcase_29 AC 57 ms
53,288 KB
testcase_30 AC 57 ms
53,280 KB
testcase_31 AC 56 ms
53,288 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package question3;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;

/**
 * No.345 最小チワワ問題 Cさんによれば、ある文字列に 'c', 'w', 'w' がこの順で含まれるとき、
 * その文字列を「チワワ列」であるといいます。 Cさんは小さなチワワが好きなので、できるだけ長さの小さいチワワ列を見つけたいです。 文字列 SS
 * が与えられるので、 その連続した部分文字列のうちチワワ列となるものの最小の長さを求めてください。
 *
 */

public class Question10_0604 {

	static final int LENGTH_MIN = 1;
	static final int LENGTH_MAX = 100;

	public static void main(String[] args) {
		InputStreamReader re = new InputStreamReader(System.in);
		BufferedReader br = new BufferedReader(re);
		ArrayList<Integer> chiwawa = new ArrayList<Integer>();
		String inputStr = null;
		int lineCount = 0;

		try {
			inputStr = br.readLine();

		} catch (IOException e) {
			System.out.println("エラーが発生しました。");
		} finally {
			try {
				re.close();
				br.close();
			} catch (IOException e) {
				System.out
						.println("InputStreamReader、BufferedReaderクローズ中にエラーが発生しました");
			}
		}

		// 有効範囲か確認
		if (LengthJudg(inputStr, LENGTH_MIN, LENGTH_MAX)) {
			while (lineCount < inputStr.length()) {

				int result = 0;
				int c_num = 0;
				int w1_num = 0;
				int w2_num = 0;

				char[] inputChar = inputStr.toCharArray();
				for (int i = lineCount; i < inputChar.length; i++) {
					// cを探す
					if (w1_num == 0 && inputChar[i] == 'c') {
						c_num = i + 1;
					}
					// wを探す
					if (c_num != 0 && inputChar[i] == 'w') {
						w1_num = i + 1;
						break;
					}
				}
				if (w1_num != 0) {
					for (int i = w1_num; i < inputChar.length; i++) {
						// wを探す(2個目)
						if (inputChar[i] == 'w') {
							w2_num = i + 1;
							break;
						}
					}

				}

				// 計算
				if (w2_num != 0) {
					 //System.out.println("c_num: " + c_num + " w1_num: " +
					 //w1_num
					 //+ " w2_num: " + w2_num);
					result = w2_num - c_num + 1;
					lineCount = c_num + 1;
				// チワワ列なし
				} else {
					result = -1;
					lineCount = inputStr.length();
				}
				// 追加
				chiwawa.add(result);
			}

			// 表示
			Collections.sort(chiwawa);
			if (chiwawa.size() >= 2 && chiwawa.get(0) == -1) {
				System.out.println(chiwawa.get(1));
			} else {
				System.out.println(chiwawa.get(0));
			}

		} else {
			System.out.println("長さが有効範囲外です");
		}

	}

	/**
	 * 有効値判定
	 *
	 * @param str
	 *            判定する文字列
	 * @param max
	 *            最大値
	 * @param min
	 *            最小値
	 * @return 範囲内ならtrue,範囲外ならfalseを返す
	 */
	private static boolean LengthJudg(String str, int min, int max) {
		Boolean result = false;
		if (min <= str.length() && str.length() <= max) {
			result = true;
		}
		return result;
	}
}
0