Catenyms Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8857 Accepted: 2336
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.gopher gopher.rat rat.tiger aloha.aloha arachnid.dog
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 ***
單詞接龍,形成歐拉路徑。
計算方法:
(1) 在輸入詞典的同時構造有向圖G,計算每一個節點所在的入度和並查集所在的根,
(2)將邊按照字典序排列。
(3) 按照遞增順序搜索每一個節點,若相鄰兩個節點屬於不同的並查集,則說明圖無法按照字典序形成若連通圖,有向歐拉路徑不存在。否則進行4
(4) 按照遞增順序搜索每一個節點,若存在入度出度相差大於1的節點,則有向圖歐拉路徑不存在,否則,若所有的節點出入度相同,則選擇序號最下的節點
作為歐拉回路的起點,若存在一個出度比入度大一的頂點,則該節點作為有向歐拉路徑的起點,
(5)從S出發dfs計算有向圖歐拉路徑
代碼:
/* *********************************************** Author :rabbit Created Time :2014/3/25 19:03:35 File Name :12.cpp ************************************************ */ #pragma comment(linker, "/STACK:102400000,102400000") #include#include #include #include #include #include #include #include #include #include #include #include #include #include