結果
| 問題 | 
                            No.345 最小チワワ問題
                             | 
                    
| コンテスト | |
| ユーザー | 
                             | 
                    
| 提出日時 | 2020-09-16 11:10:03 | 
| 言語 | Java  (openjdk 23)  | 
                    
| 結果 | 
                             
                                WA
                                 
                             
                            
                         | 
                    
| 実行時間 | - | 
| コード長 | 1,393 bytes | 
| コンパイル時間 | 1,986 ms | 
| コンパイル使用メモリ | 74,844 KB | 
| 実行使用メモリ | 54,396 KB | 
| 最終ジャッジ日時 | 2024-06-22 03:07:41 | 
| 合計ジャッジ時間 | 6,721 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge4 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 15 WA * 14 | 
ソースコード
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();
	}
}