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
The input consists of T test cases. The number of them (T) is given on the first line of the input. 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.
Output
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 should print the sentence "Ordering is possible.". Otherwise, output the sentence "The door cannot be opened.".
Sample Input
3
2
acm
ibm
3
acm
malform
mouse
2
ok
ok
Sample Output
The door cannot be opened.
Ordering is possible.
The door cannot be opened.
题目类型:欧拉回路的判定
算法分析:将每个单词的第一个字符和最后一个字符取出来作为节点,构建一个从第一个字符到最后一个字符的有向边,统计初度和入度的大小。然后判断有向图的基图是否连通(使用并查集),然后再按照定义判断欧拉回路和欧拉路径是否存在
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
#include <iostream> #include <fstream> #include <sstream> #include <algorithm> #include <iomanip> #include <cstring> #include <cstdio> #include <cmath> #include <map> #include <string> #include <vector> #include <stack> #include <queue> #include <set> #include <list> #include <ctime> using namespace std; const int INF = 0x7FFFFFFF; const int maxn = 1666; char s[maxn]; int ind[36], oud[36], parent[36]; bool used[36]; int UnFind (int val) { if (parent[val] == val) return val; return parent[val] = UnFind (parent[val]); } int main() { // freopen ("aaa.txt", "r", stdin); int t; scanf ("%d", &t); while (t--) { int n; scanf ("%d", &n); int i; for (i = 0; i < 26; i++) parent[i] = i; memset (ind, 0, sizeof (ind)); memset (oud, 0, sizeof (oud)); memset (used, false, sizeof (used)); for (i = 0; i < n; i++) { scanf ("%s", s); int len = strlen (s); ind[s[len-1]-'a']++; oud[s[0]-'a']++; used[s[len-1]-'a'] = used[s[0]-'a'] = true; if (UnFind (s[len-1] - 'a') != UnFind (s[0] - 'a')) { parent[UnFind(s[len-1]-'a')] = UnFind (s[0] - 'a'); } } bool is_valid = true; int temp; for (i = 0; i < 26; i++) if (used[i]) { temp = UnFind (i); break; } for (i = 0; i < 26; i++) if (used[i] && temp != UnFind (i)) { is_valid = false; break; } int dif = 0, val_ind = 0, val_oud = 0; for (i = 0; i < 26; i++) { if (used[i] && ind[i] != oud[i]) { dif++; if (ind[i] - oud[i] == 1) val_ind++; if (ind[i] - oud[i] == -1) val_oud++; } } if (dif != 0 && dif != 2) is_valid = false; if (dif == 2) { if (val_ind != 1 || val_oud != 1) is_valid = false; } if (is_valid) printf ("Ordering is possible.\n"); else printf ("The door cannot be opened.\n"); } return 0; } |
- « 上一篇:zoj1967
- zoj2110:下一篇 »