結果

問題 No.345 最小チワワ問題
ユーザー takutakutakutaku
提出日時 2020-09-16 11:28:15
言語 Java21
(openjdk 21)
結果
AC  
実行時間 125 ms / 2,000 ms
コード長 1,454 bytes
コンパイル時間 6,124 ms
コンパイル使用メモリ 71,832 KB
実行使用メモリ 56,132 KB
最終ジャッジ日時 2023-09-04 03:25:30
合計ジャッジ時間 7,241 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 120 ms
55,740 KB
testcase_01 AC 120 ms
55,600 KB
testcase_02 AC 120 ms
56,044 KB
testcase_03 AC 118 ms
55,796 KB
testcase_04 AC 121 ms
56,128 KB
testcase_05 AC 121 ms
55,928 KB
testcase_06 AC 120 ms
55,872 KB
testcase_07 AC 118 ms
55,732 KB
testcase_08 AC 121 ms
55,636 KB
testcase_09 AC 120 ms
55,732 KB
testcase_10 AC 122 ms
56,124 KB
testcase_11 AC 121 ms
55,656 KB
testcase_12 AC 121 ms
55,784 KB
testcase_13 AC 119 ms
55,960 KB
testcase_14 AC 122 ms
55,904 KB
testcase_15 AC 115 ms
55,776 KB
testcase_16 AC 119 ms
56,036 KB
testcase_17 AC 120 ms
55,936 KB
testcase_18 AC 119 ms
55,964 KB
testcase_19 AC 121 ms
55,728 KB
testcase_20 AC 120 ms
55,896 KB
testcase_21 AC 123 ms
55,788 KB
testcase_22 AC 119 ms
55,636 KB
testcase_23 AC 125 ms
55,600 KB
testcase_24 AC 116 ms
56,132 KB
testcase_25 AC 121 ms
55,936 KB
testcase_26 AC 120 ms
55,788 KB
testcase_27 AC 117 ms
56,048 KB
testcase_28 AC 121 ms
55,896 KB
testcase_29 AC 120 ms
55,668 KB
testcase_30 AC 120 ms
55,916 KB
testcase_31 AC 118 ms
55,512 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 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