結果

問題 No.345 最小チワワ問題
ユーザー tsunabittsunabit
提出日時 2018-05-27 21:07:10
言語 Java21
(openjdk 21)
結果
AC  
実行時間 133 ms / 2,000 ms
コード長 1,444 bytes
コンパイル時間 4,206 ms
コンパイル使用メモリ 76,556 KB
実行使用メモリ 57,680 KB
最終ジャッジ日時 2023-10-26 04:27:48
合計ジャッジ時間 8,909 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 126 ms
57,420 KB
testcase_01 AC 128 ms
57,328 KB
testcase_02 AC 128 ms
57,292 KB
testcase_03 AC 129 ms
57,580 KB
testcase_04 AC 124 ms
57,028 KB
testcase_05 AC 126 ms
57,312 KB
testcase_06 AC 125 ms
57,568 KB
testcase_07 AC 124 ms
57,488 KB
testcase_08 AC 130 ms
57,480 KB
testcase_09 AC 129 ms
57,440 KB
testcase_10 AC 131 ms
57,564 KB
testcase_11 AC 127 ms
57,416 KB
testcase_12 AC 128 ms
57,680 KB
testcase_13 AC 131 ms
57,452 KB
testcase_14 AC 127 ms
57,616 KB
testcase_15 AC 129 ms
57,436 KB
testcase_16 AC 125 ms
57,404 KB
testcase_17 AC 128 ms
57,476 KB
testcase_18 AC 133 ms
57,516 KB
testcase_19 AC 129 ms
57,556 KB
testcase_20 AC 127 ms
57,636 KB
testcase_21 AC 130 ms
57,588 KB
testcase_22 AC 126 ms
57,524 KB
testcase_23 AC 126 ms
57,396 KB
testcase_24 AC 129 ms
57,480 KB
testcase_25 AC 126 ms
57,524 KB
testcase_26 AC 126 ms
57,420 KB
testcase_27 AC 126 ms
57,448 KB
testcase_28 AC 124 ms
57,200 KB
testcase_29 AC 126 ms
55,532 KB
testcase_30 AC 127 ms
57,428 KB
testcase_31 AC 127 ms
57,444 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
import java.util.stream.Stream;

// ***問題文***
// Cさんによれば、ある文字列に 'c', 'w', 'w' がこの順で含まれるとき、その文字列を「チワワ列」であるといいます。
// Cさんは小さなチワワが好きなので、できるだけ長さの小さいチワワ列を見つけたいです。
// 文字列 S が与えられるので、その連続した部分文字列のうちチワワ列となるものの最小の長さを求めてください。
// ***入力***
// S
// 1行目に文字列 S(1≤|S|≤100)が与えられる。
// S は小文字のアルファベットのみで構成される。 
// ***出力***
// S の連続した部分文字列のうちチワワ列となるものの最小の長さを整数で出力してください。
// もしSにチワワ列が含まれない場合は−1を出力してください。
// 最後に改行してください。 

public class No345 {
    public static void main(String[] args) {
        // 標準入力から読み込む際に、Scannerオブジェクトを使う。
        Scanner sc = new Scanner(System.in);
		String s = sc.next();
		int w1 = 0, w2 = 0, ans = 1000;
		for(int i = s.length() - 1; i >= 0; i--) {
				if('w' == s.charAt(i)) {
						w2 = w1;
						w1 = i;
				}else if('c' == s.charAt(i) && w2 != 0) {
					ans = Math.min(ans, w2 - i + 1);
				}	
		}
		System.out.println(ans == 1000 ? -1 : ans);
	}
}
0