#C4361. Word Transformation Challenge
Word Transformation Challenge
Word Transformation Challenge
You are given a starting word, a target word, and a dictionary of valid words. Your task is to determine the shortest transformation sequence from the starting word to the target word. In each step, you may change exactly one letter, and each intermediate word must exist in the given dictionary.
If the transformation sequence exists, output YES
on the first line and then the sequence of words (separated by a space) on the next line. If there is no valid transformation, output NO
.
Note: The transformation must change exactly one letter at a time, and the length of all words involved is the same.
For example: converting "hit" to "cog" given the dictionary ["hot", "dot", "dog", "lot", "log", "cog"] results in:
YES hit hot dot dog cog
inputFormat
The input is read from standard input (stdin) and has the following format:
- The first line contains the start word.
- The second line contains the target word.
- The third line contains an integer n, the number of words in the dictionary.
- The next n lines each contain a word from the dictionary.
All words will have the same length.
outputFormat
The output is printed to standard output (stdout). If a valid transformation exists, print YES
on the first line and on the second line print the transformation sequence (words separated by a single space). If no valid transformation exists, print NO
.
hit
cog
6
hot
dot
dog
lot
log
cog
YES
hit hot dot dog cog
</p>