Play on words
Some of the secret doors contain a very interesting word puzzle. the team of archaeologists has to solve 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 large number of magnetic plates on every door. every plate has one word written on it. the plates must be arranged into a sequence in such a way that every word begins with the same letter as the previous word ends. for example, the word ''acm ''can be followed by the word ''motorola ''. your task is to write a computer program that will read the list of words and determine whether it is possible to arrange all of the plates in a sequence (according to the given rule) and consequently to open the door.
Input Specification
The input consistsTTest cases. The number of them (T) Is given on the first line of the input file. Each test case begins with a line containing a single integer numberNThat indicates the number of plates (1 <= n <= 100000). Then exactlyNLines follow, each containing a single word. Each word contains at least two and at most 1000 lowercase characters, that means only letters'a
'Pass'z
'Will appear in the word. The same word may appear several times in the list.
Output Specification
Your program has to determine whether it is possible to arrange all the plates in a sequence such that the first letter of each word is equal to the last letter of the previous word. all the plates from the list must be used, each exactly once. the words mentioned several times must be used that number of times.
If there exists such an ordering of plates, your program shocould print the sentence"Ordering is possible.
". Otherwise, output the sentence"The door cannot be opened.
".
Sample Input
32acmibm3acmmalformmouse2okok
Output for the sample input
The door cannot be opened.Ordering is possible.The door cannot be opened.
# Include <iostream> # include <stack> # include <cstring> # include <cstdio> # include <string> # include <algorithm> # include <queue> # include <set> # include <map> # include <fstream> # include <stack> # include <list> # include <sstream> # include <cmath> using namespace STD; # define MS (ARR, Val) memset (ARR, Val, sizeof (ARR) # define MC (DEST, Src) memcpy (DEST, SRC, sizeof (SRC )) # define n 27 # define INF 0x3fffffff # define vin T vector <int> # define setint set <int> # define mint Map <int, int> # define lint list <int> # define Sch stack <char> # define qch queue <char> # define Sint stack <int> # define qint queue <int> int in [n], out [N], Fa [N], visit [N]; char word [1005]; void Init () {MS (in, 0); MS (Out, 0 ); MS (visit, 0); MS (FA,-1);} int find (int x) {return Fa [x] =-1? X: Fa [x] = find (Fa [x]);} void _ Union (int x, int y) {int FX = find (x ), FY = find (y); If (FX! = FY) {Fa [FX] = FY ;}} bool check () {int R1, R2; R1 = 0; For (INT I = 0; I <N; I ++) {If (visit [I] & Fa [I] =-1) {R1 ++ ;}} if (R1! = 1) {return false;} R1 = R2 = 0; For (INT I = 0; I <n; I ++) {If (visit [I]) {If (in [I]! = Out [I]) {If (in [I]-out [I] = 1) {R1 ++ ;} else if (in [I]-out [I] =-1) {R2 ++;} else return false ;}} return (R1 + R2 <= 2 ); // Euler's path or Euler's loop of a directed graph} int main () {int T, N, S, E; scanf ("% d", & T); While (t --) {Init (); scanf ("% d", & N); While (n --) {scanf ("% s", word ); S = word [0]-'A'; E = word [strlen (Word)-1]-'A'; _ Union (S, e ); out [s] ++; in [e] ++; visit [s] = visit [e] = 1;} If (check ()) {printf ("ordering is possible. \ n ");} else {printf (" The door cannot be opened. \ n ") ;}} return 0 ;}