Catenyms
A catenym is a pair of words separated by a period such that the last letter of the first word is the same as the last letter of the second. For example, the following are catenyms:
dog.gopher gopher.rat rat.tiger aloha.aloha arachnid.dog
A compound catenym is a sequence of three or more words separated by periods such that each adjacent pair of words forms a catenym. For example,
aloha.aloha.arachnid.dog.gopher.rat.tiger
Given a dictionary of lower case words, you are to find a compound catenym that contains each of the words exactly once.
Input
The first line of standard input contains t, the number of test cases. Each test case begins with 3 <= n <= 1000 - the number of words in the dictionary. n distinct dictionary words follow; each word is a string of between 1 and 20 lowercase letters on a line by itself.
Output
For each test case, output a line giving the lexicographically least compound catenym that contains each dictionary word exactly once. Output "***" if there is no solution.
Sample Input
2
6
aloha
arachnid
dog
gopher
rat
tiger
3
oak
maple
elm
Sample Output
aloha.arachnid.dog.gopher.rat.tiger
***
Source
题目类型:欧拉回路
算法分析:先判断图是否是欧拉回路或通路,若是则输出时优先输出字典序较小的边。这里在建邻接表时将字典序小的边通过交换到靠近顶点表的地方来简化求解。重要的是理解Fleury算法的求解过程
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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
/************************************************* Author :supermaker Created Time :2016/3/25 19:19:54 File Location :C:\Users\abcd\Desktop\TheEternalPoet **************************************************/ #pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("aaa.txt", "r", stdin) #define CPPFF ifstream cin ("aaa.txt") #define DB(ccc) cout << #ccc << " = " << ccc << endl #define PB push_back #define MP(A, B) make_pair(A, B) typedef long long LL; typedef unsigned long long ULL; typedef double DB; typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int INF = 0x7F7F7F7F; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 1000 + 66; struct Node { int v, id, nxt; }; int head[maxn], edgelen; Node edge[maxn]; int ind[maxn], oud[maxn], par[maxn], ssta[maxn], top, n; char ss[maxn][maxn]; bool vis[maxn], used[maxn]; void Init () { memset (head, -1, sizeof (head)); memset (edge, -1, sizeof (edge)); edgelen = 0; } void AddEdge (int u, int v, int id) { edge[edgelen].v = v; edge[edgelen].id = id; edge[edgelen].nxt = head[u]; head[u] = edgelen++; int tt = head[u]; while (edge[tt].nxt != -1) { if (strcmp (ss[edge[tt].id], ss[edge[edge[tt].nxt].id]) > 0) { swap (edge[tt].v, edge[edge[tt].nxt].v); swap (edge[tt].id, edge[edge[tt].nxt].id); } tt = edge[tt].nxt; } } int UnFind (int val) { if (par[val] == val) return val; return par[val] = UnFind (par[val]); } void dfs (int u, int e) { vis[e] = true; for (int i = head[u]; i != -1; i = edge[i].nxt) if (!vis[i]) dfs (edge[i].v, i); ssta[top++] = e; } void output () { top--; printf ("%s", ss[edge[ssta[--top]].id]); while (top > 0) printf (".%s", ss[edge[ssta[--top]].id]); puts (""); } void SS () { int cntin = 0, cntout = 0, tt; bool is_valid = true, is_first = true; for (int i = 0; i < 26; i++) if (used[i]) { if (is_first) { is_first = false; tt = UnFind (i); } else { if (tt != UnFind (i)) { is_valid = false; break; } } } if (!is_valid) { puts ("***"); return ; } for (int i = 0; i < 26; i++) if (used[i]) { if (ind[i] == oud[i]) continue ; else if (ind[i] != oud[i] && abs (ind[i] - oud[i]) >= 2) { is_valid = false; break; } else if (ind[i] != oud[i] && ind[i] - oud[i] == 1) cntin++; else if (ind[i] != oud[i] && oud[i] - ind[i] == 1) cntout++; } if (!is_valid) puts ("***"); else { if ((cntin == 0 && cntout == 0) || (cntin == 1 && cntout == 1)) { int sta = -1; if (cntin == 0) { for (int i = 0; i < 26; i++) if (used[i]) { sta = i; break; } } else { for (int i = 0; i < 26; i++) if (used[i] && (oud[i] - ind[i]) == 1) { sta = i; break; } } top = 0; dfs (sta, edgelen); output (); } else puts ("***"); } } int main() { //CFF; //CPPFF; int t; scanf ("%d", &t); while (t--) { scanf ("%d", &n); Init (); memset (ind, 0, sizeof (ind)); memset (oud, 0, sizeof (oud)); memset (used, false, sizeof (used)); for (int i = 0; i < maxn; i++) par[i] = i; for (int i = 1; i <= n; i++) { scanf ("%s", ss[i]); int len = strlen (ss[i]); int u = ss[i][0] - 'a', v = ss[i][len-1] - 'a'; AddEdge (u, v, i); used[u] = used[v] = true; ind[v]++, oud[u]++; if (UnFind (u) != UnFind (v)) par[UnFind(v)] = UnFind (u); } memset (vis, false, sizeof (vis)); SS (); } return 0; } |