結果

問題 No.517 壊れたアクセサリー
ユーザー tktk_snsn
提出日時 2021-02-28 22:13:34
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 32 ms / 2,000 ms
コード長 1,040 bytes
コンパイル時間 661 ms
コンパイル使用メモリ 12,544 KB
実行使用メモリ 10,880 KB
最終ジャッジ日時 2024-10-02 22:33:37
合計ジャッジ時間 1,653 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 15
権限があれば一括ダウンロードができます

ソースコード

diff #

def atoi(a):
    return ord(a) - ord("A")


def itoa(i):
    return chr(i + ord("A"))


def main():
    N = int(input())
    S = [input() for _ in range(N)]
    M = int(input())
    T = [input() for _ in range(M)]

    if N == M == 1:
        return S[0]

    same = set(S).intersection(set(T))
    if same:
        return -1

    atob = [-1] * 26
    btoa = [-1] * 26
    for s in S:
        for a, b in zip(s[:-1], s[1:]):
            i = atoi(a)
            j = atoi(b)
            atob[i] = j
            btoa[j] = i
    for t in T:
        for a, b in zip(t[:-1], t[1:]):
            i = atoi(a)
            j = atoi(b)
            if atob[i] == -1:
                atob[i] = j
                btoa[j] = i
            elif atob[i] != j:
                return -1

    now = -1
    for i in range(26):
        if atob[i] != -1 and btoa[i] == -1:
            now = i
            break
    ans = []
    while now != -1:
        ans.append(now)
        now = atob[now]
    res = "".join(itoa(i) for i in ans)
    return res


print(main())
0