結果

問題 No.345 最小チワワ問題
ユーザー nihi9119nihi9119
提出日時 2017-06-04 22:24:28
言語 Java19
(openjdk 21)
結果
WA  
実行時間 -
コード長 3,203 bytes
コンパイル時間 4,347 ms
コンパイル使用メモリ 78,492 KB
実行使用メモリ 53,376 KB
最終ジャッジ日時 2023-10-22 04:39:13
合計ジャッジ時間 6,053 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 51 ms
52,248 KB
testcase_01 AC 51 ms
53,352 KB
testcase_02 AC 50 ms
53,344 KB
testcase_03 AC 50 ms
53,340 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 52 ms
53,344 KB
testcase_08 AC 50 ms
53,344 KB
testcase_09 AC 50 ms
53,340 KB
testcase_10 AC 50 ms
53,344 KB
testcase_11 AC 50 ms
53,364 KB
testcase_12 AC 51 ms
53,344 KB
testcase_13 AC 49 ms
53,340 KB
testcase_14 WA -
testcase_15 AC 51 ms
53,376 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 50 ms
53,344 KB
testcase_20 AC 51 ms
53,348 KB
testcase_21 AC 49 ms
53,340 KB
testcase_22 AC 51 ms
53,352 KB
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 51 ms
53,292 KB
testcase_29 WA -
testcase_30 WA -
testcase_31 AC 51 ms
53,356 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 = -1;
				int w1_num = 0;
				int w2_num = 0;

				char[] inputChar = inputStr.toCharArray();
				// cを探す
				for (int i = lineCount; i < inputChar.length; i++) {
					if (inputChar[i] == 'c') {
						c_num = i + 1;
						break;
					}
				}
				// wを探す
				if (c_num != -1) {
					for (int i = c_num; i < inputChar.length; i++) {
						if (inputChar[i] == 'w') {
							w1_num = i + 1;
							break;
						}
					}
				}
				// wを探す(2回目)
				if (w1_num != 0) {
					for (int i = w1_num; i < inputChar.length; i++) {
						if (inputChar[i] == 'w') {
							w2_num = i + 1;
							lineCount = i;
							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 = w2_num;
				// チワワ列なし
				} 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