Word Game

시간 제한2초메모리 제한2048 MB

요약
한 단어의 접미사와 다른 단어의 접두사가 겹치도록 두 단어를 합쳐, 만들 수 있는 가장 짧고 사전순으로 앞선 단어를 구한다.
난이도

보통10점 중 6점

유형
문자열, 완전 탐색, 백트래킹, 구현
정답자
아직 제출이 없습니다

문제

On the Word Game show, Ashley has selected nn words and asks Brandon to combine them. Two words ss and tt can be combined if ss has a suffix of length k>0k > 0 that is a prefix of tt. The result of combining them is a new word made of ss concatenated with the last ∣t∣−k|t|-k letters of tt. If there are multiple values of kk that are valid, any can be chosen.

Brandon must repeatedly take a pair of words from the list of words that can be combined, and replace them in the list with the combined word, until the list contains only a single word, and that word is as short as possible. If multiple final words of the same length are possible, Brandon must find the lexicographically first one.

입력

The first line of the input contains a single integer nn (1≤n≤5)(1 \le n \le 5), the number of words to start out with.

The next nn lines each contain a single word in lowercase letters of length at most 5.

출력

Output the lexicographically first word of minimum length Brandon can come up with. If it is not possible to come up with a single word, output −1-1.

예제3

  1. 예제 1

    입력
    2
    aba
    bab
    
    예상 출력
    abab
    
  2. 예제 2

    입력
    3
    ab
    bc
    ca
    
    예상 출력
    abca
    
  3. 예제 3

    입력
    2
    x
    y
    
    예상 출력
    -1