首页 > 代码库 > UVA 10129 Play on Words
UVA 10129 Play on Words
题目如下:
Play on Words
Some of the secret doorscontain a very interesting word puzzle. The team of archaeologists has tosolve it to open that doors. Because there is no other way to open the doors,the puzzle is very important for us.
There is a largenumber of magnetic plates on every door. Every plate has one word written onit. The plates must be arranged into a sequence in such a way that every wordbegins with the same letter as the previous word ends. For example, the word``acm‘‘ can be followed by the word ``motorola‘‘. Your task is towrite a computer program that will read the list of words and determine whetherit is possible to arrange all of the plates in a sequence (according tothe given rule) and consequently to open the door.
Input Specification
The inputconsists of T test cases. The number of them (T) is givenon the first line of the input file. Each test case begins with a linecontaining a single integer numberNthat indicates the numberof plates (1 <= N <= 100000). Then exactlyNlinesfollow, each containing a single word. Each word contains at least two andat most 1000 lowercase characters, that means only letters ‘a
‘through ‘z
‘ will appear in the word. The same word mayappear several times in the list.
Output Specification
Yourprogram has to determine whether it is possible to arrange all the plates ina sequence such that the first letter of each word is equal to the lastletter of the previous word. All the plates from the list must be used, eachexactly once. The words mentioned several times must be used that number oftimes.
If there exists such anordering of plates, your program should print the sentence "Ordering is possible.
". Otherwise, output the sentence "The door cannot be opened.
".
Sample Input
32acmibm3acmmalformmouse2okok
Output for the SampleInput
The door cannot be opened.Ordering is possible.The door cannot be opened.
这道题刚开始没发现是欧拉路,直接DFS,果断超时了TAT,然后改成欧拉回路,有没注意到要连通还有欧拉路可以只有一个节点是奇节点,WA了两次TAT,改过来后AC了。
AC的代码如下: