Hdoj 1116 Play on Words [merge query set] + [Euler's path], hdoj Euler's path
Play on WordsTime Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission (s): 5622 Accepted Submission (s): 1850
Problem DescriptionSome 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.
InputThe input consists of T test 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 number Nthat indicates the number of plates (1 <= N <= 100000 ). then exactly Nlines follow, each containing a single word. each word contains at least two and at most 1000 lowercase characters, that means only letters 'a' through 'Z' will appear in the word. the same word may appear several times in the list.
OutputYour 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
Sample Output
The door cannot be opened.Ordering is possible.The door cannot be opened.
Question: There are several strings. If the first character (tail character) of a string is equal to the tail character (first character) of another string, connect them, finally, I asked if I could connect all the strings.
Analysis: it is obvious that you only need to use and query the set, but it will be a little troublesome to process the first and last characters. We may consider the first and last characters of none as a vertex, if a string is an edge, this question is converted to finding whether the edge can form a connected graph. Then we need to use the Euler's path to determine whether the graph is connected.
Note: Euler's path is divided into Euler's loop and Euler's path;
Euler's path: a path that allows you to pass through each edge only once from a point.
Euler's loop: the path of the Euler's path and the last return to the origin;
For the Euler loop, the entry and location of each vertex in the graph are equal.
If the path is used, the outbound degree of the start point is reduced to 1, and the inbound degree of the end point is reduced to 1.
Code:
/* Hdoj 1116 and check the set + Euler's Pass/loop */# include <stdio. h> # include <string. h >#include <algorithm> # define M 1005int out [26], in [26], fat [26]; bool vis [26]; char s [M]; int f (int x) {if (x! = Fat [x]) fat [x] = f (fat [x]); return fat [x];} void merge (int x, int y) {int a = f (x); int B = f (y); if (! = B) fat [a] = B;} int main () {int n, t, I; scanf ("% d", & t); while (t --) {memset (vis, 0, sizeof (vis); memset (out, 0, sizeof (out); memset (in, 0, sizeof (in )); scanf ("% d", & n); for (I = 0; I <26; I ++) fat [I] = I; for (I = 0; I <n; I ++) {scanf ("% s", s); int x = s [0]-'A'; int y = s [strlen (s) -1]-'A'; merge (x, y); ++ out [x]; ++ in [y]; vis [x] = vis [y] = 1;} int flag1 = 0; for (I = 0; I <26; I ++) {// determine whether to connect if (vis [I] & fat [I] = I) ++ flag1;} if (flag1> 1) {printf ("The door cannot be opened. \ n "); continue;} int flag2, flag3; // flag1 is used to determine whether all entries and exits are equal, and flag2 is the starting point of interpretation, flag3 is the end of several flag1 = flag2 = flag3 = 0; for (I = 0; I <26; I ++) {if (vis [I] & out [I]! = In [I]) {++ flag1; if (out [I]-in [I] = 1) ++ flag2; if (in [I]-out [I] = 1) ++ flag3 ;}} if (flag1 = 0) printf ("Ordering is possible. \ n "); else if (flag1 = 2 & flag2 = 1 & flag3 = 1) printf (" Ordering is possible. \ n "); else printf (" The door cannot be opened. \ n ") ;}return 0 ;}