結果

問題 No.517 壊れたアクセサリー
ユーザー sue_charosue_charo
提出日時 2017-05-28 21:58:33
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 42 ms / 2,000 ms
コード長 1,711 bytes
コンパイル時間 93 ms
コンパイル使用メモリ 13,056 KB
実行使用メモリ 11,648 KB
最終ジャッジ日時 2024-09-21 15:25:32
合計ジャッジ時間 1,682 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 15
権限があれば一括ダウンロードができます

ソースコード

diff #

# coding: utf-8
import array, bisect, collections, copy, heapq, itertools, math, random, re, string, sys, time

sys.setrecursionlimit(10 ** 7)
INF = 10 ** 20
MOD = 10 ** 9 + 7


def II(): return int(input())
def ILI(): return list(map(int, input().split()))
def IAI(LINE): return [ILI() for __ in range(LINE)]
def IDI(): return {key: value for key, value in ILI()}


def read():
    N = II()
    A = []
    for __ in range(N):
        A.append(str(input()))
    M = II()
    B = []
    for __ in range(M):
        B.append(str(input()))
    return (N, A, M, B)


def solve(N, A, M, B):
    dict_next = collections.defaultdict(None)
    all_len = 0

    for a in A:
        all_len += len(a)
        for char_a in a:
            dict_next[char_a] = None

    def update_dict(str_list):
        for str in str_list:
            for i in range(len(str) - 1):
                dict_next[str[i]] = str[i + 1]

    update_dict(A)
    update_dict(B)

    count_none = 0
    for value in dict_next.values():
        if value is None:
            count_none += 1

    ans = ""

    if count_none != 1:
        ans = -1
    else:
        l_ans = []
        now_char = ""
        for key, value in dict_next.items():
            if value is None:
                now_char = key
        l_ans.append(now_char)
        while len(l_ans) != all_len:
            for key, value in dict_next.items():
                if value is now_char:
                    l_ans.append(key)
                    now_char = key
                    break
        else:
            l_ans.reverse()
            ans = "".join(l_ans)

    return ans


def main():
    params = read()
    print(solve(*params))


if __name__ == "__main__":
    main()
0