結果

問題 No.345 最小チワワ問題
ユーザー fkwnw3_1243
提出日時 2017-05-05 08:57:27
言語 Java
(openjdk 23)
結果
AC  
実行時間 56 ms / 2,000 ms
コード長 1,185 bytes
コンパイル時間 1,987 ms
コンパイル使用メモリ 73,620 KB
実行使用メモリ 37,208 KB
最終ジャッジ日時 2024-09-25 11:59:30
合計ジャッジ時間 4,678 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 29
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import static java.lang.System.in;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        String S = reader.readLine();
        int wCount = 0;
        int firstW = 0;
        int secondW = 0;
        int answer = Integer.MAX_VALUE;
        boolean found = false;
        for (int i = S.length() - 1; i >= 0; i--) {
            char current = S.charAt(i);
            if (current == 'w') {
                wCount += 1;
                if (wCount == 1) {
                    firstW = i;
                } else if (wCount == 2) {
                    secondW = i;
                } else {
                    firstW = secondW;
                    secondW = i;
                }
            } else if (current == 'c' && wCount >= 2) {
                answer = Math.min(answer, firstW - i + 1);
                found = true;
            }
        }
        if(found) {
            System.out.println(answer);
        } else {
            System.out.println(-1);
        }
    }
}
0