Dictionary

Time limit1sMemory limit128 MB

Summary
Given a list of words, insert leading spaces so the list satisfies a recursive definition: every maximal run of words sharing a first letter must, after removing the first word and that letter, again be a dictionary.
Level

Medium7 of 10

Topics
Trie, Recursion, Implementation, String
Solved
No attempts yet

Problem

The authors of a new all-in-one encyclopedia arranged the titles in the order they consider most suitable for their readers. This order is not always alphabetical, because they want to reveal certain peculiar relationships between the titles. Even so, they still want readers to be able to look titles up quickly.

To make this possible, they place a carefully computed number of spaces before every title in the list. They call the resulting structure a dictionary.

A dictionary is a list of words with some number of spaces before certain words. Its format is described by constraints on runs of consecutive words that start with the same letter. Every maximal run of consecutive words starting with the same letter must satisfy the following rules:

  • The first word of the run has no leading spaces. Every word after it has at least one leading space.

  • If you

    • delete the first word of the run,
    • delete one space before every remaining word, and
    • delete the first letter of every remaining word,

    then the resulting sequence is again a dictionary.

The examples below clarify this definition.

Your task is to write a program that turns a given list of words into a dictionary by adding a suitable number of spaces before certain words, while preserving the original order of the words.

Input

The input consists of at least one and at most 100000 words. Each word consists of at least one and at most 10 lower-case letters. There are no leading or trailing spaces. There are no blank lines between the words, but there may be an arbitrary number of blank lines at the end of the input.

Output

Print the original words in the same order, without any trailing spaces but with the appropriate number of leading spaces, so that the resulting list of words is a dictionary. There must be no blank lines between the words, but there may be an arbitrary number of blank lines at the end of the output.

Examples3

  1. Example 1

    Input
    a
    ant
    antique
    amaze
    bargain
    bridge
    bride
    bribe
    born
    bucket
    tart
    tan
    tram
    trolley
    t
    try
    trial
    zed
    double
    dorm
    do
    dormant
    donate
    again
    agony
    boost
    back
    born
    
    Expected output
    a
     ant
      antique
     amaze
    bargain
     bridge
      bride
       bribe
     born
     bucket
    tart
     tan
     tram
      trolley
     t
     try
      trial
    zed
    double
     dorm
      do
      dormant
      donate
    again
     agony
    boost
     back
     born
    
  2. Example 2

    Input
    hello
    
    Expected output
    hello
    
  3. Example 3

    Input
    apple
    banana
    cherry
    date
    
    Expected output
    apple
    banana
    cherry
    date