結果

問題 No.345 最小チワワ問題
ユーザー takutaku
提出日時 2020-09-16 11:28:15
言語 Java
(openjdk 23)
結果
AC  
実行時間 123 ms / 2,000 ms
コード長 1,454 bytes
コンパイル時間 1,819 ms
コンパイル使用メモリ 75,028 KB
実行使用メモリ 54,396 KB
最終ジャッジ日時 2024-06-22 03:07:59
合計ジャッジ時間 6,258 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 29
権限があれば一括ダウンロードができます

ソースコード

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 max = 0;
		
		for(int i = 0; i < s.length; i++) {
		    
		    // cが存在したとき、文字列の何番目に存在したかを保存する
		    if(s[i].equals("c")) {
		        countC = i;
		        
		        // wカウントのリセット
        		int countWW = 0;
		        
		        // 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