This page is still under construction.

Parts of this page are still being built. What you see may change.

Typing Practice

Interview

Time limit8sMemory limit1024 MB

Summary
Given up to six strings over the letters A to D, find the shortest string that contains every input string as a subsequence, breaking ties lexicographically.
Level

Medium7 of 10

Topics
Dynamic programming, Bit manipulation, BFS, String
Solved
No attempts yet

Problem

Mingyu and Myeongjin are having a typing practice contest. The typing practice consists of NN words, and each word is made up only of the uppercase letters A through D. Starting from the first letter of a word, typing the correct letter for the current position advances to the next letter, and after typing all the letters, pressing Enter moves on to the next word. Because the program is forgiving of typos, typing a wrong letter is simply ignored with no penalty. In other words, viewing the words as sequences, a word is typed successfully if the given word is a subsequence of the word formed by collecting the typed letters.

Mingyu, unable to beat Myeongjin due to lack of skill, decides to cheat. Discovering that the program is lax enough to allow pasting, he plans to create a single word and keep pasting it to pass through all the words in an instant. That is, viewing the words as sequences, Mingyu's word must have all NN words as subsequences. Since his typing speed is slow, he wants the length of that word to be minimal. Write a program to find the word Mingyu should use.

Input

The first line gives the number of words NN. (1≤N≤6)(1 \leq N \leq 6)

Over the following NN lines, the strings SiS_i representing the typing practice words are given. SiS_i consists only of the uppercase letters A, B, C, D and has length at most 99.

Output

Print the string to use on the first line. If there are several possible strings, print the lexicographically smallest one.

Examples3

  1. Example 1

    Input
    5
    AACABBA
    BACBA
    DDDBBACB
    CCCCBABD
    DDDDDDD
    
    Expected output
    AABCACCCDDDBBACBDDDD
    
  2. Example 2

    Input
    6
    AAAAAAAAA
    BBBBBBBBB
    CCCCCCCCC
    DDDDDDDDD
    ABABABABA
    CDCDCDCDC
    
    Expected output
    AAAAABABABABABBBBBCCCCCDCDCDCDCDDDDD
    
  3. Example 3

    Input
    6
    AAAAAAAAA
    BBBBBBBBB
    CCCCCCCCC
    DDDDDDDDD
    ACDBABBAC
    CBDDDBBAA
    
    Expected output
    AAAAAAABBBBCBCCCCCCCDDDBBABBACDDDDDD