結果

問題 No.345 最小チワワ問題
ユーザー spaciaspacia
提出日時 2016-06-02 13:15:10
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,527 bytes
コンパイル時間 2,693 ms
コンパイル使用メモリ 77,988 KB
実行使用メモリ 41,924 KB
最終ジャッジ日時 2024-04-16 23:03:09
合計ジャッジ時間 7,668 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 128 ms
41,308 KB
testcase_01 AC 126 ms
41,316 KB
testcase_02 AC 127 ms
41,276 KB
testcase_03 AC 116 ms
40,140 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 130 ms
41,148 KB
testcase_08 AC 130 ms
41,560 KB
testcase_09 AC 128 ms
41,124 KB
testcase_10 AC 130 ms
41,332 KB
testcase_11 AC 129 ms
41,148 KB
testcase_12 AC 128 ms
41,116 KB
testcase_13 AC 115 ms
39,944 KB
testcase_14 WA -
testcase_15 AC 124 ms
41,452 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 113 ms
40,168 KB
testcase_21 AC 126 ms
41,060 KB
testcase_22 AC 131 ms
41,664 KB
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 131 ms
41,460 KB
testcase_29 WA -
testcase_30 WA -
testcase_31 AC 127 ms
41,272 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayList;
import java.util.Scanner;

public class Main {

	public static void main (String[] args) {

		Scanner sc = new Scanner(System.in);
		String in = sc.nextLine();

		System.out.println(searchMinChiwawaLength(in));

		sc.close();

	}

	// 部分文字列のうちチワワ列となるものの最小の長さを求める
	public static int searchMinChiwawaLength (String in) {

		// 戻り値
		int min_chiwawa_str_length = -1;

		// 入力文字列中の'c'と'w'の添え字をそれぞれ格納する
		ArrayList<Integer> clocate_of_in = new ArrayList<Integer>();
		ArrayList<Integer> wlocate_of_in = new ArrayList<Integer>();

		for (int i = 0; i < in.length(); i++) {
			if (in.charAt(i) == 'c') clocate_of_in.add(i);
			if (in.charAt(i) == 'w') wlocate_of_in.add(i);
		}

		// 入力文字列中に含まれる'c'と'w'の数
		int csize = clocate_of_in.size();
		int wsize = wlocate_of_in.size();

		for (int ci = 0; ci < csize; ci++) {

			// 'c'の位置を取得
			int clocate = clocate_of_in.get(ci);
			// clocateより右にある'w'の数
			int wcnt = 0;

			for (int wi = 0; wi < wsize; wi++) {

				// 'w'の位置を取得
				int wlocate = wlocate_of_in.get(wi);

				// 'c'より右にある'w'をカウント
				if (clocate < wlocate) wcnt++;

				// 'c'より右に2つの'w'を見つけた場合
				if (wcnt == 2) {
					min_chiwawa_str_length =
							Math.max(min_chiwawa_str_length, wlocate - clocate + 1);
					break;
				}
			}

		}

		return min_chiwawa_str_length;
	}

}
0