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
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
思路:这道题目和我之前写过的 P1341 无序字母对(欧拉回路)很相似,基本上是一个套路,不过这个题目比后者复杂一些,还得求是否联通,建议先去看后面一个题目,参悟完了再来看这个题目,首先我们可以看出来这道是让我们求欧拉路径的,那么我们先来建立图,字符串的起点到终点又一条有向的路径,然后前向星建立图,在建立图前需要先排序,建立图的时候要从后往前插入,因为前向星是从后往前读取的,所以我们反正插入就可以得到字典序列的输出,然后就是欧拉路径的判断了,这个题目和欧拉路径有区别的地方在于所有的字符串只输出一次,而欧拉路径是所有的边都要输出一次,如果咱们用欧拉路径的方法去做可能会产生输出重复字符串的错误,所以这里我们用一个flag来记录改字符串是记录过,接下来看代码
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<cstdio>
using namespace std;
struct node{
int to, index;
bool flag;
int next;
}edge[2010];
int head[30], cnt = 1, index, ans[2010], n, m, in[30], out[30];
void add(int u,int v,int index)
{
edge[cnt].to = v;
edge[cnt].next = head[u];
edge[cnt].index = index;
edge[cnt].flag = false;
head[u] = cnt++;
}
void init() { //初始化
fill(head, head + 30, 0);
fill(in, in + 30, 0);
fill(out, out + 30, 0);
cnt = 1;
index = 0;
}
void dfs(int u) {
for (int i = head[u]; i != 0; i = edge[i].next) {
int v = edge[i].to;
if (!edge[i].flag) {
edge[i].flag = true;
dfs(v);
ans[index++] = edge[i].index;
}
}
}
int main() {
cin >> n;
while (n--) {
scanf("%d",&m);
vector<string> v;
string s;
for(int i = 0;i < m;i++) {
cin >> s;
v.push_back(s);
}
sort(v.begin(), v.end());//排序
init();
int st = 1000;
for(int i = m - 1; i >= 0; i--)//重点,一定要逆着插入
{
int u = v[i][0] - 'a';
int vv = v[i][v[i].length() - 1] - 'a';
add(u,vv,i);
out[u]++;
in[vv]++;
st = min(u, min(vv, st)); //找最小的点
}
int cc1 = 0, cc2 = 0;
for(int i = 0;i < 26;i++)
{
if(out[i] - in[i] == 1) // 如果出度比入读大1,那么改点为起点
{
cc1++;
st = i;
}
else if(out[i] - in[i] == -1)
cc2++;
else if(out[i] - in[i] != 0)
cc1 = 3;
}
if(! ( (cc1 == 0 && cc2 == 0) || (cc1 == 1 && cc2 == 1) )) //如果所有顶点初度等于入读或者只有起点和终点不符合这条规矩,那么进行dfs,否则输出***
{
cout << "***" << endl;
continue;
}
dfs(st);
if(index != m) // 判断图的联通,也就是数组里面的节点个数是不是等于输入的个数
{
cout << "***" << endl;
continue;
}
for(int i = index-1; i >= 0;i--)
{
cout<<v[ans[i]];
if(i > 0) cout << ".";
}
cout << endl;
}
return 0;
}
解释完毕