結果

問題 No.205 マージして辞書順最小
ユーザー kenkooookenkoooo
提出日時 2015-05-08 23:49:20
言語 Java19
(openjdk 21)
結果
AC  
実行時間 268 ms / 5,000 ms
コード長 1,519 bytes
コンパイル時間 3,742 ms
コンパイル使用メモリ 74,612 KB
実行使用メモリ 57,664 KB
最終ジャッジ日時 2023-09-19 09:02:15
合計ジャッジ時間 6,611 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 122 ms
55,884 KB
testcase_01 AC 124 ms
55,404 KB
testcase_02 AC 165 ms
56,908 KB
testcase_03 AC 169 ms
56,532 KB
testcase_04 AC 167 ms
56,672 KB
testcase_05 AC 171 ms
56,544 KB
testcase_06 AC 127 ms
55,712 KB
testcase_07 AC 182 ms
57,376 KB
testcase_08 AC 179 ms
56,720 KB
testcase_09 AC 180 ms
57,008 KB
testcase_10 AC 264 ms
57,664 KB
testcase_11 AC 268 ms
57,380 KB
testcase_12 AC 222 ms
57,536 KB
testcase_13 AC 190 ms
56,588 KB
testcase_14 AC 128 ms
55,404 KB
testcase_15 AC 125 ms
55,760 KB
testcase_16 AC 125 ms
55,392 KB
testcase_17 AC 129 ms
55,712 KB
testcase_18 AC 128 ms
55,844 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		char[][] list = new char[N][];
		int sum = 0;
		for (int i = 0; i < list.length; i++) {
			list[i] = sc.next().toCharArray();
			sum += list[i].length;
		}
		sc.close();

		int[] curs = new int[N];
		StringBuilder sb = new StringBuilder();
		boolean[] dropped = new boolean[N];
		while (sb.length() < sum) {
			Arrays.fill(dropped, false);

			int k = 0;
			int min = -1;
			while (true) {
				char c = 'z' + 1;
				for (int i = 0; i < list.length; i++) {
					if (dropped[i]) {
						continue;
					}
					if (curs[i] + k >= list[i].length) {
						continue;
					}
					if (c >= list[i][curs[i] + k]) {
						min = i;
						c = list[i][curs[i] + k];
					}
				}
				for (int i = 0; i < list.length; i++) {
					if (dropped[i]) {
						continue;
					}
					if (curs[i] + k >= list[i].length) {
						dropped[i] = true;
						continue;
					}
					if (c < list[i][curs[i] + k]) {
						dropped[i] = true;
					}
				}
				int cnt = 0;
				for (int i = 0; i < list.length; i++) {
					if (!dropped[i]) {
						cnt++;
					}
				}
				if (cnt == 0) {
					break;
				} else if (cnt == 1) {
					for (int i = 0; i < dropped.length; i++) {
						if (!dropped[i]) {
							min = i;
							break;
						}
					}
					break;
				}
				k++;
			}

			sb.append(list[min][curs[min]]);
			curs[min]++;
		}
		System.out.println(sb.toString());

	}
}
0