結果

問題 No.517 壊れたアクセサリー
ユーザー htensaihtensai
提出日時 2020-02-18 08:02:35
言語 Java21
(openjdk 21)
結果
AC  
実行時間 56 ms / 2,000 ms
コード長 1,276 bytes
コンパイル時間 2,200 ms
コンパイル使用メモリ 78,660 KB
実行使用メモリ 50,320 KB
最終ジャッジ日時 2024-04-16 04:03:48
合計ジャッジ時間 4,020 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 54 ms
50,288 KB
testcase_01 AC 54 ms
49,756 KB
testcase_02 AC 53 ms
49,880 KB
testcase_03 AC 53 ms
50,320 KB
testcase_04 AC 54 ms
50,188 KB
testcase_05 AC 54 ms
49,860 KB
testcase_06 AC 54 ms
50,172 KB
testcase_07 AC 53 ms
50,264 KB
testcase_08 AC 54 ms
49,892 KB
testcase_09 AC 53 ms
50,192 KB
testcase_10 AC 54 ms
50,248 KB
testcase_11 AC 54 ms
50,200 KB
testcase_12 AC 53 ms
49,964 KB
testcase_13 AC 56 ms
50,272 KB
testcase_14 AC 54 ms
49,896 KB
testcase_15 AC 55 ms
50,060 KB
testcase_16 AC 55 ms
50,184 KB
testcase_17 AC 54 ms
50,288 KB
testcase_18 AC 54 ms
49,896 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
import java.io.*;

public class Main {
	public static void main (String[] args) throws Exception {
	    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	    int n = Integer.parseInt(br.readLine());
	    int[] next = new int[26];
	    int size = 0;
	    Arrays.fill(next, -1);
	    HashSet<Integer> starts = new HashSet<>();
	    for (int i = 0; i < n; i++) {
	        char[] arr = br.readLine().toCharArray();
	        size += arr.length;
	        for (int j = 0; j < arr.length - 1; j++) {
                next[arr[j] - 'A'] = arr[j + 1] - 'A';
	        }
	        starts.add(arr[0] - 'A');
	    }
	    int m = Integer.parseInt(br.readLine());
	    for (int i = 0; i < m; i++) {
	        char[] arr = br.readLine().toCharArray();
	        for (int j = 0; j < arr.length - 1; j++) {
                next[arr[j] - 'A'] = arr[j + 1] - 'A';
	        }
	    }
	    for (int x : starts) {
	        StringBuilder sb = new StringBuilder();
	        sb.append((char)(x + 'A'));
	        while (next[x] != -1) {
	            x = next[x];
	            sb.append((char)(x + 'A'));
	        }
	        if (sb.length() == size) {
	            System.out.println(sb);
	            return;
	        }
	    }
	    System.out.println(-1);
    }
}
0