結果

問題 No.345 最小チワワ問題
ユーザー eagleeagle
提出日時 2022-06-14 11:08:44
言語 Java
(openjdk 23)
結果
WA  
実行時間 -
コード長 869 bytes
コンパイル時間 2,019 ms
コンパイル使用メモリ 74,788 KB
実行使用メモリ 56,792 KB
最終ジャッジ日時 2024-10-02 05:09:15
合計ジャッジ時間 7,155 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 20 WA * 9
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner sc = new Scanner(System.in);
		//与えられる文字列
		String S = sc.next();
		char[] chiwawa = {'c', 'w', 'w'};
		//配列chiwawaを参照するインデックス
		int idx = 0;
		//チワワ列を数える
		int ans = 0;
		//チワワ列が始まったか
		boolean flg = false;
		boolean finish = false;
		for(int i = 0; i < S.length(); i++) {
			char c = S.charAt(i);
			if(c == chiwawa[0]) {
				idx = 1;
				flg = true;
				ans = 0;
			} else if(c == chiwawa[idx]) {
				idx++;
			}
			if(flg) {
				ans++;
			}
			//チワワ列が終わったか
			if(idx == chiwawa.length) {
				finish = true;
				break;
			}
		}
		if(finish) {
			System.out.println(ans);
		} else {
			System.out.println(-1);
		}
	}
}
0