結果

問題 No.517 壊れたアクセサリー
ユーザー brthyyjpbrthyyjp
提出日時 2021-09-19 15:02:33
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 94 ms / 2,000 ms
コード長 1,570 bytes
コンパイル時間 298 ms
コンパイル使用メモリ 87,184 KB
実行使用メモリ 71,944 KB
最終ジャッジ日時 2023-09-14 10:42:19
合計ジャッジ時間 3,218 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 93 ms
71,764 KB
testcase_01 AC 92 ms
71,532 KB
testcase_02 AC 89 ms
71,676 KB
testcase_03 AC 94 ms
71,752 KB
testcase_04 AC 90 ms
71,760 KB
testcase_05 AC 88 ms
71,944 KB
testcase_06 AC 91 ms
71,348 KB
testcase_07 AC 88 ms
71,556 KB
testcase_08 AC 89 ms
71,684 KB
testcase_09 AC 89 ms
71,680 KB
testcase_10 AC 89 ms
71,620 KB
testcase_11 AC 91 ms
71,868 KB
testcase_12 AC 88 ms
71,600 KB
testcase_13 AC 89 ms
71,908 KB
testcase_14 AC 88 ms
71,624 KB
testcase_15 AC 89 ms
71,656 KB
testcase_16 AC 93 ms
71,624 KB
testcase_17 AC 88 ms
71,684 KB
testcase_18 AC 88 ms
71,572 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
def cycle_detectable_topological_sort(g, ind):
    V = len(g)
    order = []
    depth = [-1]*V
    for i in range(V):
        if not ind[i]:
            order.append(i)
            depth[i] = 0

    q = deque(order)
    while q:
        v = q.popleft()
        cur_depth = depth[v]
        for u in g[v]:
            ind[u] -= 1
            if not ind[u]:
                depth[u] = max(depth[u], cur_depth+1)
                q.append(u)
                order.append(u)
    if len(order) == V:
        return (order, depth)
    else:
        return (None, None)

n = int(input())
A = [str(input()) for i in range(n)]
m = int(input())
B = [str(input()) for i in range(m)]

g = [[] for i in range(26)]
ind = [0]*26
E = set()
for a in A:
    for i in range(len(a)-1):
        c1 = ord(a[i])-ord('A')
        c2 = ord(a[i+1])-ord('A')
        g[c1].append(c2)
        ind[c2] += 1
        E.add((c1, c2))
for b in B:
    for i in range(len(b)-1):
        c1 = ord(b[i])-ord('A')
        c2 = ord(b[i+1])-ord('A')
        g[c1].append(c2)
        ind[c2] += 1
        E.add((c1, c2))

order, _  = cycle_detectable_topological_sort(g, ind)
used = set()
for a in A:
    for c in a:
        c = ord(c)-ord('A')
        used.add(c)
for b in B:
    for c in b:
        c = ord(c)-ord('A')
        used.add(c)
order = [i for i in order if i in used]
for k in range(len(order)-1):
    c1 = order[k]
    c2 = order[k+1]
    if (c1, c2) not in E:
        print(-1)
        exit()
else:
    ans = [chr(i+ord('A')) for i in order]
    print(''.join(ans))
0