結果

問題 No.345 最小チワワ問題
ユーザー nihi9119nihi9119
提出日時 2017-06-06 18:42:10
言語 Java21
(openjdk 21)
結果
AC  
実行時間 61 ms / 2,000 ms
コード長 2,586 bytes
コンパイル時間 4,083 ms
コンパイル使用メモリ 79,388 KB
実行使用メモリ 53,304 KB
最終ジャッジ日時 2023-10-26 04:11:23
合計ジャッジ時間 6,885 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

package test_4;

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 Question_10_0606 {

	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;
		int result = 0;
		int c_num = 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)) {

			//c検索
			c_num = inputStr.indexOf('c');

			while (c_num != -1) {

				int w1_num = 0;
				int w2_num = 0;

				//wを探す (検索済みの部分は除く)
				inputStr = inputStr.substring(c_num + 1);
				w1_num = inputStr.indexOf("w");
				//wを探す(2回目)
				String w1String = inputStr.substring(w1_num + 1);
				w2_num = w1String.indexOf("w");
				if (w2_num != -1) {
					chiwawa.add(w1_num + w2_num + 3);
				}
				c_num = inputStr.indexOf("c");
			}

			// 表示
			if (chiwawa.isEmpty()) {
				System.out.println(-1);
			} else {
				Collections.sort(chiwawa);
				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