結果

問題 No.517 壊れたアクセサリー
ユーザー sue_charosue_charo
提出日時 2017-05-28 21:58:33
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 38 ms / 2,000 ms
コード長 1,711 bytes
コンパイル時間 167 ms
コンパイル使用メモリ 12,028 KB
実行使用メモリ 10,956 KB
最終ジャッジ日時 2023-10-21 14:11:17
合計ジャッジ時間 1,809 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
10,956 KB
testcase_01 AC 36 ms
10,956 KB
testcase_02 AC 35 ms
10,956 KB
testcase_03 AC 35 ms
10,956 KB
testcase_04 AC 36 ms
10,956 KB
testcase_05 AC 37 ms
10,956 KB
testcase_06 AC 38 ms
10,956 KB
testcase_07 AC 35 ms
10,956 KB
testcase_08 AC 36 ms
10,956 KB
testcase_09 AC 36 ms
10,956 KB
testcase_10 AC 36 ms
10,956 KB
testcase_11 AC 36 ms
10,956 KB
testcase_12 AC 36 ms
10,956 KB
testcase_13 AC 35 ms
10,956 KB
testcase_14 AC 35 ms
10,956 KB
testcase_15 AC 36 ms
10,956 KB
testcase_16 AC 35 ms
10,956 KB
testcase_17 AC 36 ms
10,956 KB
testcase_18 AC 35 ms
10,956 KB
権限があれば一括ダウンロードができます

ソースコード

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