結果

問題 No.205 マージして辞書順最小
ユーザー thorikawathorikawa
提出日時 2015-05-08 23:44:06
言語 Java21
(openjdk 21)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,649 bytes
コンパイル時間 2,673 ms
コンパイル使用メモリ 76,668 KB
実行使用メモリ 348,620 KB
最終ジャッジ日時 2023-09-19 08:58:21
合計ジャッジ時間 42,094 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 127 ms
55,648 KB
testcase_01 AC 128 ms
56,144 KB
testcase_02 AC 690 ms
88,924 KB
testcase_03 AC 684 ms
88,468 KB
testcase_04 AC 664 ms
86,520 KB
testcase_05 AC 620 ms
86,316 KB
testcase_06 AC 181 ms
57,132 KB
testcase_07 AC 4,838 ms
348,056 KB
testcase_08 TLE -
testcase_09 AC 4,760 ms
346,780 KB
testcase_10 AC 4,759 ms
348,556 KB
testcase_11 AC 4,835 ms
348,208 KB
testcase_12 AC 4,838 ms
348,172 KB
testcase_13 AC 4,815 ms
348,620 KB
testcase_14 AC 217 ms
59,348 KB
testcase_15 AC 130 ms
55,460 KB
testcase_16 AC 164 ms
56,436 KB
testcase_17 AC 173 ms
56,696 KB
testcase_18 AC 164 ms
56,688 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;

public class Main {
    public static void main(String[] argv) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        String[] ss = new String[n];
        for (int i = 0; i < n; ++i) {
            ss[i] = scanner.next();
        }
        String res = ss[0];
        for (int i = 1; i < n; ++i) {
            int len1 = res.length();
            int len2 = ss[i].length();
            String[][] dp = new String[len1 + 1][len2 + 1];
            dp[0][0] = "";
            for (int j = 0; j < len1 + 1; j++) {
                for (int k = 0; k < len2 + 1; k++) {
                    if (j == 0 && k == 0) continue;
                    String s1 = null;
                    String s2 = null;
                    if (k > 0) {
                        s1 = dp[j][k - 1] + ss[i].charAt(k - 1);
                    }
                    if (j > 0) {
                        s2 = dp[j - 1][k] + res.charAt(j - 1);
                    }
                    if (s1 == null) dp[j][k] = s2;
                    else if (s2 == null) dp[j][k] = s1;
                    else {
                        if (s1.compareTo(s2) > 0) {
//                            System.out.println(String.format("dp[%d][%d]=%s", j, k, s2));
                            dp[j][k] = s2;
                        } else {
//                            System.out.println(String.format("dp[%d][%d]=%s", j, k, s1));
                            dp[j][k] = s1;
                        }
                    }
                }
            }
            res = dp[len1][len2];
        }
        System.out.println(res);
    }
}
0