Блог пользователя gfonn

Автор gfonn, 8 лет назад, По-английски

Hi everyone! I can't solve this problem, can you help me please?

Statement:

Given a list of words, you have to find an order in which a word A can be before to word B only if the last letter of A is the same that the first letter of B. This order must be circular, like this:

(LEFT: Given words. RIGHT: A possible ordering of the words)

Input: First line, the quantity of words N (1 <= N <= 9000). Next N lines have the words (one word per line). Lenght of each word don't exceed 6:

6
arbol
orden
susana
otro
listo
nexos

Output: If it's possible to make the circular ordering, print N lines with the words ordered (as the ordering is circular, you can start listing them with any word). If it's impossible, print "Impossible"

susana
arbol
listo
otro
orden
nexos

FULL STATEMENT (IN SPANISH)

The first thing I know is that for finding the ordering you only need two letters (first and last of each word). I was thinking in Hamiltonian Cycle, but the input is too big.

  • Проголосовать: нравится
  • +2
  • Проголосовать: не нравится

»
8 лет назад, # |
  Проголосовать: нравится +18 Проголосовать: не нравится

Hint: it is hard to find a Hamiltonian cycle but easy to find Eulerian cycle. So, construct a graph so that you have to do the latter.

»
8 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Construct a graph such that a->b exist if last character of a is same as first character of b .try finding a cycle of length N

»
8 лет назад, # |
  Проголосовать: нравится +1 Проголосовать: не нравится

I think it's almost like this problem.

You can find many solutions on google, like this one.