結果

問題 No.345 最小チワワ問題
ユーザー nihi9119nihi9119
提出日時 2017-06-04 20:34:45
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 2,543 bytes
コンパイル時間 3,657 ms
コンパイル使用メモリ 77,664 KB
実行使用メモリ 53,540 KB
最終ジャッジ日時 2023-10-22 04:24:15
合計ジャッジ時間 7,714 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 53 ms
53,436 KB
testcase_01 AC 55 ms
53,420 KB
testcase_02 AC 54 ms
53,432 KB
testcase_03 AC 54 ms
52,340 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 54 ms
53,440 KB
testcase_08 AC 54 ms
53,416 KB
testcase_09 AC 54 ms
53,420 KB
testcase_10 AC 54 ms
53,420 KB
testcase_11 AC 53 ms
53,428 KB
testcase_12 AC 53 ms
53,416 KB
testcase_13 AC 53 ms
52,316 KB
testcase_14 WA -
testcase_15 AC 52 ms
50,460 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 54 ms
53,424 KB
testcase_20 AC 54 ms
53,416 KB
testcase_21 AC 55 ms
53,416 KB
testcase_22 AC 56 ms
53,424 KB
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 54 ms
53,424 KB
testcase_29 WA -
testcase_30 WA -
testcase_31 AC 54 ms
53,420 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package question3;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * 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);

		try {
			String inputStr = br.readLine();
			int result = 0;
			int c_num = 100;
			int w1_num = 0;
			int w2_num = 0;

			//有効範囲か確認
			if (LengthJudg(inputStr, LENGTH_MIN, LENGTH_MAX)){
				char[] inputChar = inputStr.toCharArray();
				//cを探す
				for (int i = 0; i < inputChar.length; i++) {
					if (inputChar[i] == 'c') {
						c_num = i + 1;
						break;
					}
				}
				//wを探す
				if (c_num != 100) {
					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;
							break;
						}
					}
				}

				//計算
				if (w2_num != 0) {
					result = w2_num - c_num + 1;
				//チワワ列なし
				} else {
					result= -1;
				}
				System.out.println(result);

 			} else {
				System.out.println("長さが有効範囲外です");
			}
		} catch (IOException e) {
			System.out.println("エラーが発生しました。");
		} finally {
			try {
				re.close();
				br.close();
			} catch (IOException e) {
				System.out.println("InputStreamReader、BufferedReaderクローズ中にエラーが発生しました");
			}
		}
	}

	/**
	 * 有効値判定
	 * @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