結果
問題 | No.345 最小チワワ問題 |
ユーザー |
![]() |
提出日時 | 2017-05-05 08:54:35 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 54 ms / 2,000 ms |
コード長 | 1,368 bytes |
コンパイル時間 | 2,049 ms |
コンパイル使用メモリ | 74,580 KB |
実行使用メモリ | 50,408 KB |
最終ジャッジ日時 | 2024-09-25 11:58:55 |
合計ジャッジ時間 | 4,694 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 29 |
ソースコード
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; } // System.out.println("first ; " + firstW + " second; " + secondW); // } else if (current == 'c' && wCount >= 2) { // System.out.println("c; " + i + "first ; " + firstW + " second; " + secondW); answer = Math.min(answer, firstW - i + 1); found = true; } } if(found) { System.out.println(answer); } else { System.out.println(-1); } } }