Programming contests

ECN selejtező programozó csapatverseny, 2014. április 27.

April 27, 2014 10:30 AM – April 27, 2014 3:30 PM

Word Transformation

A common word puzzle found in many newspapers and magazines is the word transformation. By taking a starting word and successively altering a single letter to make a new word, one can build a sequence of words which changes the original word to a given end word. For instance, the word “spice” can be transformed in four steps to the word “stock” according to the following sequence: spice, slice, slick, stick, stock. Each successive word differs from the previous word in only a single character position, while the word length remains the same.

Given a dictionary of words from which to make transformations, plus a list of starting and ending words, your team is to write a program to determine the number of steps in the shortest possible transformation.

Input Specification

The first line of the input is an integer N, indicating the number of test sets that your correct program should test, followed by a blank line. Each test set will have two sections. The first section will be the dictionary of available words with one word per line, terminated by a line containing an asterisk (*) rather than a word. There can be up to 200 words in the dictionary; all words will be alphabetic and in lowercase, and no word will be longer than ten characters. Words can appear in the dictionary in any order.

Following the dictionary are pairs of words, one pair per line, with the words in the pair separated by a single space. These pairs represent the starting and ending words in a transformation. All pairs are guaranteed to have a transformation using the dictionary given. The starting and ending words will appear in the dictionary.

Two consecutive input sets will be separated by a blank line.

Output Specification

The output should contain one line per word pair for each test set and must include the starting word, the ending word, and the number of steps in the shortest possible transformation, separated by single spaces. Two consecutive output sets should be separated by a blank line.

Sample Input

  1. 1
  2. dip
  3. lip
  4. mad
  5. map
  6. maple
  7. may
  8. pad
  9. pip
  10. pod
  11. pop
  12. sap
  13. sip
  14. slice
  15. slick
  16. spice
  17. stick
  18. stock
  19. *
  20. spice stock
  21. may pod
download as text file

Output for Sample Input

  1. spice stock 4
  2. may pod 3
download as text file
University of Debrecen; Faculty of Informatics; v. 03/01/2019