POJ 2337 Catenyms (有向圖歐拉通路),pojcatenyms
Catenyms
| Time Limit: 1000MS |
|
Memory Limit: 65536K |
| Total Submissions: 9914 |
|
Accepted: 2588 |
Description
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.gophergopher.ratrat.tigeraloha.alohaarachnid.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
26alohaarachniddoggopherrattiger3oakmapleelm
Sample Output
aloha.arachnid.dog.gopher.rat.tiger***
Source
Waterloo local 2003.01.25
題目連結:http://poj.org/problem?id=2337
題目大意:t組範例,每組n個字串,如果一個字串的尾字元和另一個的頭字元相同則可以串連,問能不能將這n個都串連起來,能的話按要求輸出結果,不能輸出***
題目分析:本題構圖思路很簡單,一個字串的第一個字元和最後一個字元為結點,字串本身為邊,因為本題沒說連通,我們要先用並查集判連通,若根不唯一則說明不連通,接著就是看點的出入度,某結點出入度相差大於1或相差等於1的多於2個則不存在歐拉通路,否則若存在歐拉通路,則把出度比入度大1的結點當起點,若存在歐拉迴路,則取字典序最小的做為起點。本體要求最後答案的字典序最小,因此我們再判斷前要先對字串由小到大排序,至於找通路,直接DFS,本題邊很少,所以可以直接通過邊來找
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;struct EDGE{ char s[25]; int x, y; }e[1005];bool has[30], vis[1005];int in[30], out[30], fa[30], ans[1005];int n, st, len;bool cmp(EDGE a, EDGE b){ return strcmp(a.s, b.s) < 0;}int abs(int x){ return x > 0 ? x : -x;}int change(char ch){ return int(ch - 'a' + 1);}void UF_set(){ for(int i = 0; i < 200; i++) fa[i] = i;}int Find(int x){ return fa[x] == x ? x : fa[x] = Find(fa[x]);}void Union(int a, int b){ int r1 = Find(a); int r2 = Find(b); if(r1 != r2) fa[r1] = r2;}bool exist(){ int t = -1; for(int i = 1; i <= 26; i++) { if(has[i]) { if(t == -1) t = Find(i); if(t != Find(i)) return false; } } int sum = 0, tmp = 100; for(int i = 1; i <= 26; i++) { if(has[i]) { tmp = min(tmp, i); if(in[i] != out[i]) { if(abs(in[i] - out[i]) > 1) return false; if(out[i] > in[i]) st = i; sum ++; } } } if(sum > 2) return false; if(sum == 0) st = tmp; return true;}void DFS(int now){ for(int i = 0; i < n; i++) { if(!vis[i] && e[i].x == now) { vis[i] = true; DFS(e[i].y); ans[len++] = i; } }}int main(){ int T; scanf("%d", &T); while(T--) { st = 0; UF_set(); memset(has, false, sizeof(has)); memset(in, 0, sizeof(in)); memset(out, 0, sizeof(out)); scanf("%d", &n); for(int i = 0; i < n; i++) { scanf("%s", e[i].s); e[i].x = change(e[i].s[0]); e[i].y = change(e[i].s[(int)strlen(e[i].s) - 1]); has[e[i].x] = true; has[e[i].y] = true; in[e[i].y] ++; out[e[i].x] ++; Union(e[i].x, e[i].y); } sort(e, e + n, cmp); if(exist()) { len = 0; memset(vis, false, sizeof(vis)); DFS(st); for(int i = len - 1; i > 0; i--) printf("%s.", e[ans[i]].s); printf("%s\n", e[ans[0]].s); } else printf("***\n"); }}