結果

問題 No.204 ゴールデン・ウィーク(2)
ユーザー r.suzukir.suzuki
提出日時 2015-12-31 23:17:24
言語 Java21
(openjdk 21)
結果
AC  
実行時間 50 ms / 1,000 ms
コード長 2,524 bytes
コンパイル時間 3,690 ms
コンパイル使用メモリ 79,480 KB
実行使用メモリ 49,992 KB
最終ジャッジ日時 2023-08-25 23:46:54
合計ジャッジ時間 7,843 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 49 ms
49,592 KB
testcase_01 AC 50 ms
49,568 KB
testcase_02 AC 49 ms
49,380 KB
testcase_03 AC 49 ms
49,724 KB
testcase_04 AC 49 ms
49,716 KB
testcase_05 AC 49 ms
49,688 KB
testcase_06 AC 49 ms
49,728 KB
testcase_07 AC 49 ms
49,720 KB
testcase_08 AC 49 ms
49,552 KB
testcase_09 AC 50 ms
49,568 KB
testcase_10 AC 50 ms
47,480 KB
testcase_11 AC 48 ms
49,592 KB
testcase_12 AC 49 ms
49,724 KB
testcase_13 AC 50 ms
49,484 KB
testcase_14 AC 48 ms
49,660 KB
testcase_15 AC 50 ms
49,856 KB
testcase_16 AC 48 ms
49,388 KB
testcase_17 AC 49 ms
49,932 KB
testcase_18 AC 49 ms
49,832 KB
testcase_19 AC 50 ms
49,548 KB
testcase_20 AC 50 ms
49,976 KB
testcase_21 AC 50 ms
49,552 KB
testcase_22 AC 49 ms
49,808 KB
testcase_23 AC 50 ms
49,628 KB
testcase_24 AC 50 ms
49,992 KB
testcase_25 AC 50 ms
49,668 KB
testcase_26 AC 49 ms
49,468 KB
testcase_27 AC 50 ms
49,756 KB
testcase_28 AC 50 ms
49,560 KB
testcase_29 AC 49 ms
49,524 KB
testcase_30 AC 48 ms
49,536 KB
testcase_31 AC 49 ms
49,532 KB
testcase_32 AC 47 ms
49,608 KB
testcase_33 AC 50 ms
49,540 KB
testcase_34 AC 48 ms
49,500 KB
testcase_35 AC 48 ms
49,504 KB
testcase_36 AC 48 ms
49,568 KB
testcase_37 AC 49 ms
49,608 KB
testcase_38 AC 48 ms
49,624 KB
testcase_39 AC 48 ms
49,484 KB
testcase_40 AC 47 ms
49,472 KB
testcase_41 AC 48 ms
49,592 KB
testcase_42 AC 49 ms
49,548 KB
testcase_43 AC 47 ms
49,552 KB
testcase_44 AC 47 ms
49,528 KB
testcase_45 AC 48 ms
49,676 KB
testcase_46 AC 49 ms
49,616 KB
testcase_47 AC 50 ms
49,536 KB
testcase_48 AC 48 ms
49,584 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class No_204 {
	static char holiday;
	static char weekday;

	// 連続する平日の数を返すメソッド
	static int countWeekday(StringBuilder sb, int i, int c, int v) {
		/*
		 * 最後の文字が渡されたとき、 または判定箇所の文字がholidayだった時、 または連続する休日数が有休数を超えた時終了する。
		 */
		if (i == sb.length() - 1 || sb.charAt(i + 1) == holiday || c >= v) {
			return c;
		} else {
			return countWeekday(sb, ++i, ++c, v);
		}
	}

	// 連続する休日の数を返すメソッド
	static int countHoliday(StringBuilder sb, int i, int c) {
		/*
		 * 最後の文字が渡されたとき、 または判定箇所の文字がweekdayだった時終了する。
		 */
		if (i == sb.length() - 1 || sb.charAt(i + 1) == weekday) {
			return c;
		} else {
			return countHoliday(sb, ++i, ++c);
		}
	}

	// 連続する休日の最大数を返すメソッド
	static int countMaxHoliday(StringBuilder sb, int c) {
		int max = 0;
		for (int i = 0; i < sb.length();) {
			if (sb.charAt(i) == holiday) {
				int m = countHoliday(sb, i, 1);
				max = Math.max(max, m);
				i += m;
			} else {
				i++;
			}
		}
		return max;
	}

	public static void main(String[] args) throws IOException {
		ArrayList<StringBuilder> list = new ArrayList<StringBuilder>();
		InputStreamReader in = new InputStreamReader(System.in);
		BufferedReader br = new BufferedReader(in);

		int vacation = Integer.parseInt(br.readLine());
		StringBuilder calendar = new StringBuilder(br.readLine() + br.readLine());
		holiday = 'o';
		weekday = 'x';
		int maxHoliday = 0;

		// 2週間の前後に有休の数だけ平日を追加
		for (int i = 0; i < vacation; i++) {
			calendar.insert(0, weekday);
			calendar.append(weekday);
		}

		if (vacation <= 0) {
			list.add(calendar);
		} else {
			for (int i = 0; i < calendar.length(); i++) {
				if (calendar.charAt(i) == weekday) {
					int count = countWeekday(calendar, i, 1, vacation);
					StringBuilder weeks = new StringBuilder(calendar);// コピーを作成
					for (int j = 0; j < count; j++) {
						weeks.setCharAt(i + j, holiday);
					}
					list.add(weeks);// コピーをリストに格納
				}
			}
		}

		for (int i = 0; i < list.size(); i++) {
			int m = countMaxHoliday(list.get(i), 0);
			maxHoliday = Math.max(maxHoliday, m);
		}
		System.out.println(maxHoliday);
		br.close();

	}

}
0