結果

問題 No.205 マージして辞書順最小
ユーザー thorikawathorikawa
提出日時 2015-05-08 23:40:13
言語 Java21
(openjdk 21)
結果
AC  
実行時間 4,980 ms / 5,000 ms
コード長 1,649 bytes
コンパイル時間 4,127 ms
コンパイル使用メモリ 76,428 KB
実行使用メモリ 350,568 KB
最終ジャッジ日時 2023-09-19 08:56:37
合計ジャッジ時間 41,865 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 129 ms
55,848 KB
testcase_01 AC 128 ms
55,664 KB
testcase_02 AC 692 ms
88,452 KB
testcase_03 AC 694 ms
88,876 KB
testcase_04 AC 655 ms
86,536 KB
testcase_05 AC 620 ms
86,392 KB
testcase_06 AC 175 ms
56,900 KB
testcase_07 AC 4,980 ms
348,476 KB
testcase_08 AC 4,924 ms
348,932 KB
testcase_09 AC 4,841 ms
348,808 KB
testcase_10 AC 4,828 ms
348,652 KB
testcase_11 AC 4,819 ms
348,228 KB
testcase_12 AC 4,795 ms
350,568 KB
testcase_13 AC 4,820 ms
348,940 KB
testcase_14 AC 217 ms
57,324 KB
testcase_15 AC 129 ms
56,080 KB
testcase_16 AC 167 ms
57,016 KB
testcase_17 AC 173 ms
56,852 KB
testcase_18 AC 168 ms
56,664 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