結果

問題 No.345 最小チワワ問題
ユーザー SagToki
提出日時 2018-05-17 14:21:24
言語 Java
(openjdk 23)
結果
WA  
実行時間 -
コード長 1,689 bytes
コンパイル時間 3,659 ms
コンパイル使用メモリ 79,596 KB
実行使用メモリ 41,556 KB
最終ジャッジ日時 2024-06-28 13:29:01
合計ジャッジ時間 8,841 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 15 WA * 14
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class MinimumChihuahua {
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        try{
            String S = scanner.next();
            if(S.length() < 1 || S.length() > 100){
                System.out.println("文字列は1以上100以下の文字数で入力してください");
                System.exit(0);
            }
            Pattern pattern = Pattern.compile(".*[A-Z].*");
            Matcher matcher = pattern.matcher(S);
            if(matcher.find()){
                S = S.toLowerCase();
            }
            
            char[] CS = S.toCharArray();
            ArrayList<Integer> Stock = new ArrayList<>();
            int CountW = 0;
            
            for (int i = 0 ; i < S.length() ;i++){
                if(CS[i] == 'c'){
                    for(int j = i ; j < S.length() ; j++){
                        if(CS[j] == 'w'){
                            CountW++;
                            if(CountW == 2){
                                 Stock.add(j - i + 1);
                                 break;
                             }
                        }
                    }
                }
            }
            
            if(Stock.isEmpty()){
                System.out.println("-1");
            }else{
                Collections.sort(Stock);
                System.out.println(Stock.get(0));
            }
              
        }catch(Exception E){
            System.out.println("予期せぬエラーです");
        }
    }
}
0