結果

問題 No.345 最小チワワ問題
ユーザー mitsuomitsuo
提出日時 2016-05-05 11:27:45
言語 Java
(openjdk 23)
結果
AC  
実行時間 130 ms / 2,000 ms
コード長 1,108 bytes
コンパイル時間 2,809 ms
コンパイル使用メモリ 79,612 KB
実行使用メモリ 41,924 KB
最終ジャッジ日時 2024-09-25 11:44:59
合計ジャッジ時間 7,610 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 29
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayList;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		List<String> input = new ArrayList<>();
		while (sc.hasNext()) {
			input.add(sc.nextLine());
		}

		System.out.println(solve(input));

		sc.close();
	}

	public static int solve(List<String> in) {
		String s = in.get(0);
		int INF = Integer.MAX_VALUE;
		int ans = INF;
		Queue<Integer> cp = new PriorityQueue<>();
		List<Integer> wp = new ArrayList<>();
		char[] ll = s.toCharArray();
		for (int i = 0; i < ll.length; i++) {
			if (ll[i] == 'c') {
				cp.add(i);
			} else if (ll[i] == 'w') {
				wp.add(i);
			}
		}

		while (!cp.isEmpty()) {
			int cplace = cp.poll();
			for (int i = 0; i < wp.size() - 1; i++) {
				int w1place = wp.get(i);
				if (w1place < cplace) {
					continue;
				} else {
					int w2place = wp.get(i + 1);
					ans = Math.min(ans, w2place - cplace + 1);
				}
			}
		}

		if (ans == INF) {
			return -1;
		} else {
			return ans;
		}
	}

}
0