結果

問題 No.345 最小チワワ問題
ユーザー tentententen
提出日時 2020-11-11 14:27:28
言語 Java21
(openjdk 21)
結果
AC  
実行時間 121 ms / 2,000 ms
コード長 912 bytes
コンパイル時間 5,008 ms
コンパイル使用メモリ 72,048 KB
実行使用メモリ 56,128 KB
最終ジャッジ日時 2023-09-30 00:31:00
合計ジャッジ時間 8,911 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 115 ms
56,128 KB
testcase_01 AC 117 ms
55,624 KB
testcase_02 AC 118 ms
55,700 KB
testcase_03 AC 116 ms
55,600 KB
testcase_04 AC 116 ms
55,388 KB
testcase_05 AC 115 ms
55,664 KB
testcase_06 AC 115 ms
55,584 KB
testcase_07 AC 115 ms
55,624 KB
testcase_08 AC 121 ms
55,592 KB
testcase_09 AC 117 ms
55,348 KB
testcase_10 AC 116 ms
55,932 KB
testcase_11 AC 114 ms
53,304 KB
testcase_12 AC 118 ms
53,972 KB
testcase_13 AC 116 ms
55,736 KB
testcase_14 AC 118 ms
55,888 KB
testcase_15 AC 118 ms
55,952 KB
testcase_16 AC 116 ms
55,948 KB
testcase_17 AC 115 ms
55,516 KB
testcase_18 AC 117 ms
55,944 KB
testcase_19 AC 115 ms
55,420 KB
testcase_20 AC 115 ms
56,000 KB
testcase_21 AC 117 ms
55,744 KB
testcase_22 AC 116 ms
55,640 KB
testcase_23 AC 115 ms
55,580 KB
testcase_24 AC 118 ms
55,708 KB
testcase_25 AC 116 ms
55,356 KB
testcase_26 AC 119 ms
55,964 KB
testcase_27 AC 115 ms
55,584 KB
testcase_28 AC 117 ms
55,396 KB
testcase_29 AC 120 ms
55,712 KB
testcase_30 AC 120 ms
55,592 KB
testcase_31 AC 116 ms
53,964 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        char[] arr = sc.next().toCharArray();
        TreeSet<Integer> cPosition = new TreeSet<>();
        TreeSet<Integer> wPosition = new TreeSet<>();
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == 'c') {
                cPosition.add(i);
            } else if (arr[i] == 'w') {
                wPosition.add(i);
            }
        }
        wPosition.add(Integer.MAX_VALUE - 2);
        wPosition.add(Integer.MAX_VALUE - 1);
        int min = Integer.MAX_VALUE;
        for (int x : cPosition) {
            int y = wPosition.higher(wPosition.higher(x));
            min = Math.min(min, y - x + 1);
        }
        if (min > arr.length) {
            System.out.println(-1);
        } else {
            System.out.println(min);
        }
    }
}
0