SMSU Computer Problems

Programming Problem #22
"Rare Order"

Depth First Search Problems

A rare book collector recently discovered a book written in an unfamiliar language that used the same letters as the English language. The book contained a short index, but the ordering of the items in the index was different from what one would expect if the characters were ordered the same way as in the English alphabet. The collector tried to use the index to determine the ordering of the characters (the collating sequence), but gave up in frustration. Your job is to write a program that, given an index in sorted order, will determine the collating sequence.

The input consists of one or more indices. Each index consists of at least one and at most 100 words followed by a line containing a pound sign '#'. No word contains more than 20 characters. The words in an index consist only of lowercase English letters, and are listed in sorted order according to some collating sequence. Not all characters will necessarily appear in an index, but an index will determine a unique ordering among the characters that do appear. For each index, output a single string that specifies the collating sequence for the characters appearing in that index. Each index is a separate problem.

Example Input

<BOF>
xwy
zx
zxy
zxw
ywwx
#
q
a
#
frog
forg
fgro
gorf
gfor
#
<EOF>

Example Output

xzyw
qa
rofg

A version of this problem originally appeared in the 1990 ACM finals.

Dr. Eric Shade (eds231f@cnas.smsu.edu)

Solution (Yogy Namara) :

This problem is known as topological sorting a directed-acyclic graph.
yogy-n@sby.mega.net.id GeoCities