結果

問題 No.345 最小チワワ問題
ユーザー takutakutakutaku
提出日時 2020-09-16 11:10:03
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,393 bytes
コンパイル時間 2,453 ms
コンパイル使用メモリ 72,076 KB
実行使用メモリ 58,612 KB
最終ジャッジ日時 2023-09-04 03:25:07
合計ジャッジ時間 7,731 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 117 ms
55,716 KB
testcase_01 AC 119 ms
55,836 KB
testcase_02 AC 119 ms
55,840 KB
testcase_03 AC 117 ms
55,680 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 118 ms
55,428 KB
testcase_08 AC 117 ms
55,736 KB
testcase_09 AC 117 ms
55,908 KB
testcase_10 AC 114 ms
55,456 KB
testcase_11 AC 119 ms
55,564 KB
testcase_12 AC 120 ms
55,600 KB
testcase_13 AC 120 ms
55,700 KB
testcase_14 WA -
testcase_15 AC 120 ms
55,800 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 117 ms
55,796 KB
testcase_20 AC 117 ms
55,520 KB
testcase_21 AC 125 ms
55,684 KB
testcase_22 AC 120 ms
55,796 KB
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 117 ms
55,460 KB
testcase_29 WA -
testcase_30 WA -
testcase_31 AC 117 ms
56,040 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
	public static void main(String[] args) {
	    
	    // 入力文字列を取得
		Scanner sc = new Scanner(System.in);
		String str = sc.nextLine();
		
		// 1文字ずつの配列にする
		String[] s = str.split("",0);
		
		int countC = 0;
		int countWW = 0;
		int max = 0;
		
		for(int i = 0; i < s.length; i++) {
		    
		    // cが存在したとき、文字列の何番目に存在したかを保存する
		    if(s[i].equals("c")) {
		        countC = i;
		        
		        // cが存在してから、wが存在するかを探索する
		        for(int j = i; j < s.length; j++) {
		            if(s[j].equals("w")) {
		                countWW++;
		                
		                // wが2個存在したとき、cwwの文字列の長さを計算する
		                if(countWW == 2){
		                    countWW = j;
		                    int num = countWW - countC;
		                    
		                    // 計算結果の辻褄合わせ
		                    if(max == 0) {
		                        max = num + 1;
		                    }else if(max > num) {
		                        max = num + 1;
		                    }
		                }
		            }
		        }
		    }
		}
		
		// 出力処理
		if(max == 0) {
		    System.out.println("-1");
		}else{
		    System.out.println(max);
		}
		
		sc.close();
	}
}
0