結果

問題 No.517 壊れたアクセサリー
ユーザー GrenacheGrenache
提出日時 2017-05-28 22:13:29
言語 Java21
(openjdk 21)
結果
AC  
実行時間 141 ms / 2,000 ms
コード長 1,616 bytes
コンパイル時間 4,050 ms
コンパイル使用メモリ 79,628 KB
実行使用メモリ 57,676 KB
最終ジャッジ日時 2023-10-21 14:16:46
合計ジャッジ時間 7,720 ms
ジャッジサーバーID
(参考情報)
judge10 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 136 ms
57,676 KB
testcase_01 AC 137 ms
57,504 KB
testcase_02 AC 138 ms
57,424 KB
testcase_03 AC 140 ms
57,676 KB
testcase_04 AC 140 ms
57,444 KB
testcase_05 AC 137 ms
57,496 KB
testcase_06 AC 137 ms
57,428 KB
testcase_07 AC 135 ms
57,420 KB
testcase_08 AC 136 ms
57,384 KB
testcase_09 AC 137 ms
57,444 KB
testcase_10 AC 137 ms
57,452 KB
testcase_11 AC 136 ms
57,480 KB
testcase_12 AC 138 ms
57,520 KB
testcase_13 AC 138 ms
57,512 KB
testcase_14 AC 137 ms
57,104 KB
testcase_15 AC 139 ms
57,460 KB
testcase_16 AC 141 ms
57,384 KB
testcase_17 AC 141 ms
57,492 KB
testcase_18 AC 137 ms
57,424 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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


public class Main_yukicoder517 {

	private static Scanner sc;
	private static Printer pr;

	private static void solve() {
		int n = sc.nextInt();

		char[][] a = new char[n][];
		for (int i = 0; i < n; i++) {
			a[i] = sc.next().toCharArray();
		}

		int m = sc.nextInt();

		char[][] b = new char[m][];
		for (int i = 0; i < m; i++) {
			b[i] = sc.next().toCharArray();
		}

		int[] next = new int[26];
		Arrays.fill(next, -2);
		for (char[] s : a) {
			for (int i = 0, size = s.length - 1; i < size; i++) {
				next[s[i] - 'A'] = s[i + 1] - 'A';
			}

			next[s[s.length - 1] - 'A'] = -1;
		}

		for (char[] s : b) {
			for (int i = 0, size = s.length - 1; i < size; i++) {
				next[s[i] - 'A'] = s[i + 1] - 'A';
			}
		}

		Set<Integer> hs = new HashSet<>();
		int cnt = 0;
		for (int i = 0, size = next.length; i < size; i++) {
			if (next[i] == -1) {
				cnt++;
			}

			if (next[i] != -2) {
				hs.add(next[i]);
			}
		}

		if (cnt > 1) {
			pr.println("-1");
			return;
		}

		for (int i = 0, size = next.length; i < size; i++) {
			if (next[i] != -2 && !hs.contains(i)) {
				StringBuilder ret = new StringBuilder();
				int j = i;
				while (j >= 0) {
					ret.append((char)(j + 'A'));
					j = next[j];
				}

				pr.println(ret);
				break;
			}
		}
	}

	// ---------------------------------------------------
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		pr = new Printer(System.out);

		solve();

		pr.close();
		sc.close();
	}

	private static class Printer extends PrintWriter {
		Printer(PrintStream out) {
			super(out);
		}
	}
}
0