結果

問題 No.345 最小チワワ問題
ユーザー eagleeagle
提出日時 2022-06-14 11:26:02
言語 Java21
(openjdk 21)
結果
AC  
実行時間 135 ms / 2,000 ms
コード長 1,112 bytes
コンパイル時間 2,198 ms
コンパイル使用メモリ 74,344 KB
実行使用メモリ 54,456 KB
最終ジャッジ日時 2024-04-10 04:14:37
合計ジャッジ時間 7,032 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 118 ms
53,984 KB
testcase_01 AC 116 ms
53,888 KB
testcase_02 AC 119 ms
54,160 KB
testcase_03 AC 109 ms
52,992 KB
testcase_04 AC 127 ms
54,288 KB
testcase_05 AC 127 ms
53,976 KB
testcase_06 AC 117 ms
54,156 KB
testcase_07 AC 123 ms
54,208 KB
testcase_08 AC 120 ms
54,008 KB
testcase_09 AC 103 ms
52,548 KB
testcase_10 AC 121 ms
54,200 KB
testcase_11 AC 122 ms
54,292 KB
testcase_12 AC 125 ms
54,172 KB
testcase_13 AC 135 ms
53,900 KB
testcase_14 AC 130 ms
54,032 KB
testcase_15 AC 110 ms
53,560 KB
testcase_16 AC 119 ms
54,456 KB
testcase_17 AC 118 ms
54,240 KB
testcase_18 AC 116 ms
54,032 KB
testcase_19 AC 118 ms
54,028 KB
testcase_20 AC 110 ms
53,160 KB
testcase_21 AC 122 ms
54,004 KB
testcase_22 AC 122 ms
54,024 KB
testcase_23 AC 106 ms
52,956 KB
testcase_24 AC 122 ms
54,148 KB
testcase_25 AC 119 ms
54,048 KB
testcase_26 AC 119 ms
54,208 KB
testcase_27 AC 118 ms
54,200 KB
testcase_28 AC 121 ms
54,288 KB
testcase_29 AC 123 ms
54,144 KB
testcase_30 AC 122 ms
54,076 KB
testcase_31 AC 116 ms
53,988 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner sc = new Scanner(System.in);
		//与えられる文字列
		String S = sc.next();
		//チワワ列の長さ
		int ans = Integer.MAX_VALUE;
		//チワワ列を見つけたか
		boolean find = false;
		//cを探す
		for(int i = 0; i < S.length(); i++) {
			char c = S.charAt(i);
			//cを見つけたら
			if(c == 'c') {
				int length = 1;
				//wを二回探す
				boolean finish = false; //チワワ列が終わったか
				boolean findW = false; //wを見つけたか
				for(int j = i + 1; j < S.length(); j++) {
					char w = S.charAt(j);
					length++;
					//wを見つけたら
					if(w == 'w') {
						//二回目なら終了
						if(findW) {
							finish = true;
							break;
						}
						//一回目の場合
						findW = true;
					}
				}
				if(finish && ans > length) {
					ans = length;
					find = true;
				}
			}
		}
		//チワワ列を見つけられていない場合
		if(!find) {
			ans = -1;
		}
		System.out.println(ans);
	}
}
0