結果

問題 No.517 壊れたアクセサリー
ユーザー H3PO4H3PO4
提出日時 2020-09-09 08:58:53
言語 Python3
(3.11.6 + numpy 1.26.0 + scipy 1.11.3)
結果
AC  
実行時間 17 ms / 2,000 ms
コード長 687 bytes
コンパイル時間 513 ms
コンパイル使用メモリ 10,764 KB
実行使用メモリ 8,324 KB
最終ジャッジ日時 2023-08-21 02:06:31
合計ジャッジ時間 1,523 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
7,944 KB
testcase_01 AC 17 ms
8,168 KB
testcase_02 AC 16 ms
7,796 KB
testcase_03 AC 16 ms
7,768 KB
testcase_04 AC 16 ms
7,864 KB
testcase_05 AC 15 ms
7,888 KB
testcase_06 AC 16 ms
8,220 KB
testcase_07 AC 15 ms
8,268 KB
testcase_08 AC 16 ms
8,208 KB
testcase_09 AC 16 ms
7,820 KB
testcase_10 AC 16 ms
8,172 KB
testcase_11 AC 16 ms
7,840 KB
testcase_12 AC 16 ms
7,792 KB
testcase_13 AC 16 ms
7,804 KB
testcase_14 AC 16 ms
7,812 KB
testcase_15 AC 16 ms
7,784 KB
testcase_16 AC 15 ms
8,264 KB
testcase_17 AC 16 ms
8,324 KB
testcase_18 AC 15 ms
7,796 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

N = int(input())
A = [input() for _ in range(N)]
M = int(input())
B = [input() for _ in range(M)]

G = {s: set() for a in A for s in a}
first_dict = {s: True for s in G.keys()}

for a in A:
    for i in range(len(a) - 1):
        G[a[i]].add(a[i + 1])
        first_dict[a[i + 1]] = False
for b in B:
    for i in range(len(b) - 1):
        G[b[i]].add(b[i + 1])
        first_dict[b[i + 1]] = False


def is_impossible(b):
    if b:
        print(-1)
        exit()


first = [k for k, v in first_dict.items() if v]
is_impossible(len(first) != 1)

cur = first[0]
ans = cur
for _ in range(len(G) - 1):
    is_impossible(len(G[cur]) != 1)
    cur = G[cur].pop()
    ans += cur
print(ans)
0