Under construction

Still being built. Things may change

Superwords

Interview

Time limit1sMemory limit1024 MB

Summary
Given N words, find the shortest superword that contains each word as a subword in the given order, with start and end letters strictly increasing.
Level

Medium5 of 10

Topics
Greedy, String matching, String
Solved
No attempts yet

Problem

The cows are playing dictionary games again. There is not much else to do while chewing one's cud. The new game is a simple one, but they need to verify their answers.

Given a list of N (1≤N≤1001 \le N \le 100) words, what is the smallest "superword" that contains all of them, in order, as subwords? Consider an example using three words: 'big', 'green', and 'engine'. To make a "superword" (which, regrettably, rarely appears in the dictionary), one combines them carefully to yield: 'bigreengine'.

Words appear sequentially in a superword when the first letter of a constituent word appears strictly later in the superword than the first letter of the previous word, and the last letter of that word appears strictly later in the superword than the last letter of the previous word. Here are some two word examples:

  • sin in -> sinin
  • sin sine -> sinsine
  • sin int -> sint

Your task is to find superwords.

Input

  • Line 1: A single integer, N
  • Lines 2..N+1: Each line contains a single word that contains only lower case letters ('a'..'z'). No word is longer than 75 characters.

Output

A single line that contains the "superword".

Examples1

  1. Example 1

    Input
    3
    big
    green
    engine
    
    Expected output
    bigreengine