結果

問題 No.517 壊れたアクセサリー
ユーザー tentententen
提出日時 2020-12-10 16:45:32
言語 Java21
(openjdk 21)
結果
AC  
実行時間 129 ms / 2,000 ms
コード長 1,745 bytes
コンパイル時間 2,414 ms
コンパイル使用メモリ 79,528 KB
実行使用メモリ 57,624 KB
最終ジャッジ日時 2023-10-20 00:48:22
合計ジャッジ時間 5,625 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 127 ms
57,624 KB
testcase_01 AC 113 ms
56,320 KB
testcase_02 AC 124 ms
57,508 KB
testcase_03 AC 124 ms
57,408 KB
testcase_04 AC 123 ms
57,396 KB
testcase_05 AC 125 ms
57,560 KB
testcase_06 AC 124 ms
57,588 KB
testcase_07 AC 123 ms
57,348 KB
testcase_08 AC 125 ms
57,564 KB
testcase_09 AC 124 ms
57,240 KB
testcase_10 AC 116 ms
56,328 KB
testcase_11 AC 128 ms
57,456 KB
testcase_12 AC 128 ms
57,020 KB
testcase_13 AC 128 ms
57,576 KB
testcase_14 AC 129 ms
57,392 KB
testcase_15 AC 128 ms
57,468 KB
testcase_16 AC 128 ms
57,508 KB
testcase_17 AC 119 ms
56,336 KB
testcase_18 AC 126 ms
57,276 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        HashSet<Integer> starts = new HashSet<>();
        int[] next = new int[26];
        Arrays.fill(next, -1);
        boolean[] used = new boolean[26];
        for (int i = 0; i < n; i++) {
            char[] arr = sc.next().toCharArray();
            starts.add(arr[0] - 'A');
            used[arr[0] - 'A'] = true;
            for (int j = 0; j < arr.length - 1; j++) {
                next[arr[j] - 'A'] = arr[j + 1] - 'A';
                used[arr[j + 1] - 'A'] = true;
            }
        }
        int count = 0;
        for (boolean b : used) {
            if (b) {
                count++;
            }
        }
        int m = sc.nextInt();
        for (int i = 0; i < m; i++) {
            char[] arr = sc.next().toCharArray();
            starts.add(arr[0] - 'A');
            for (int j = 0; j < arr.length - 1; j++) {
                if (next[arr[j] - 'A'] >= 0 && next[arr[j] - 'A'] != arr[j + 1] - 'A') {
                    System.out.println(-1);
                    return;
                }
                next[arr[j] - 'A'] = arr[j + 1] - 'A';
            }
        }
        for (int x : starts) {
            int cur = x;
            int c = 0;
            StringBuilder sb = new StringBuilder();
            sb.append((char)(cur + 'A'));
            while (next[cur] >= 0) {
                cur = next[cur];
                sb.append((char)(cur + 'A'));
                c++;
            }
            if (c == count - 1) {
                System.out.println(sb);
                return;
            }
        }
        System.out.println(-1);
    }
}
0