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
*** 思路:
可以将每个单词看作是两点一边,就变成判断欧拉路径(回路)了,从连通性和出度入度两方面判断,然后打印欧拉路径即可,代码如下(注释
int in[], out[], ans[], start, fa[], n, k;

struct edge {
int u, v, vis;
string s;
bool operator<(const edge &a) const {
return s<a.s;
}
} Edge[]; void init() {
memset(in, , sizeof(in)), memset(out, , sizeof(out));
memset(ans, , sizeof(ans));
k = ;
for(int i = ; i <= ; ++i)
fa[i] = i;
} // Find-Union
int Find(int u) {
if(fa[u] != u)
return fa[u] = Find(fa[u]);
return fa[u];
} void Union(int x, int y) {
x = Find(x), y = Find(y);
if(x != y) fa[x] = y;
} // judge whether connected bool Connect() {
int now = Find(start);
for(int i = ; i <= ; ++i)
if(in[i] || out[i])
if(Find(i) != now)
return false;
return true;
} // judge the in and out and set the start bool check() {
int t1 = , t2 = , i;
for(i = ; i <= ; ++i) {
if(in[i] == out[i]) continue;
else if(in[i] == out[i] + )
t1++;
else if(in[i] == out[i] - ) {
t2++;
start = i;
}
else
break;
}
if(i == && t1 == t2 && (t1 == || t1 == )) {
return true;
}
return false;
} //print the euler path
void dfs(int v) {
for(int i = ; i <= n; ++i) {
if(Edge[i].vis == && Edge[i].u == v) {
Edge[i].vis = ;
dfs(Edge[i].v);
ans[k++] = i;
}
}
} int main() {
ios::sync_with_stdio(false);
int T;
cin >> T;
while(T--) {
cin >> n;
init();
for(int i = ; i <= n; ++i) {
int u, v;
cin >> Edge[i].s;
u = Edge[i].s[] - 'a' + ;
v = Edge[i].s[Edge[i].s.size()-] - 'a' + ;
in[v]++, out[u]++;
Edge[i].u = u, Edge[i].v = v, Edge[i].vis = ;
Union(u, v);
}
sort(Edge+, Edge++n);
start = Edge[].u;
if(check() && Connect()) {
dfs(start);
for(int i = n-; i >= ; --i)
cout << Edge[ans[i]].s << ".";
cout << Edge[ans[]].s << "\n";
} else {
cout <<"***\n";
}
}
return ;
}
 

Day 4 -E - Catenyms POJ - 2337的更多相关文章

  1. Catenyms POJ - 2337(单词+字典序输出路径)

    题意: 就是给出几个单词 看能否组成欧拉回路或路径  当然还是让输出组成的最小字典序的路 解析: 还是把首尾字母看成点   把单词看成边 记录边就好了 这题让我对fleury输出最小字典序又加深了一些 ...

  2. POJ 2337 Catenyms

    http://poj.org/problem?id=2337 题意: 判断给出的单词能否首尾相连,输出字典序最小的欧拉路径. 思路: 因为要按字典序大小输出路径,所以先将字符串排序,这样加边的时候就会 ...

  3. POJ 2337 Catenyms(欧拉回(通)路:路径输出+最小字典序)

    题目链接:http://poj.org/problem?id=2337 题目大意:给你n个字符串,只有字符串首和尾相同才能连接起来.请你以最小字典序输出连接好的单词. 解题思路:跟POJ1386一个意 ...

  4. poj 2337 Catenyms 【欧拉路径】

    题目链接:http://poj.org/problem?id=2337 题意:给定一些单词,假设一个单词的尾字母与还有一个的首字母同样则能够连接.问能否够每一个单词用一次,将全部单词连接,能够则输出字 ...

  5. POJ 2337 Catenyms (有向图欧拉路径,求字典序最小的解)

    Catenyms Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8756   Accepted: 2306 Descript ...

  6. POJ 2337 Catenyms (欧拉回路)

    Catenyms Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8173   Accepted: 2149 Descript ...

  7. POJ 2337 Catenyms(有向图的欧拉通路)

    题意:给n个字符串(3<=n<=1000),当字符串str[i]的尾字符与str[j]的首字符一样时,可用dot连接.判断用所有字符串一次且仅一次,连接成一串.若可以,输出答案的最小字典序 ...

  8. POJ 2337 Catenyms(有向欧拉图:输出欧拉路径)

    题目链接>>>>>> 题目大意: 给出一些字符串,问能否将这些字符串  按照 词语接龙,首尾相接  的规则 使得每个字符串出现一次 如果可以 按字典序输出这个字符串 ...

  9. POJ 2337 Catenyms (欧拉图)

    本文链接http://i.cnblogs.com/EditPosts.aspx?postid=5402042 题意: 给你N个单词,让你把这些单词排成一个序列,使得每个单词的第一个字母和上一个字单词的 ...

随机推荐

  1. char、pchar、string互相转换

    1.string转换成pchar 可以使用pchar进行强制类型转换,也可以使用StrPCopy函数 var s:string; p,p1:PChar; begin s:='Hello Delphi' ...

  2. idea设置自带的maven为国内镜像

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/panchang199266/articl ...

  3. 【PAT甲级】1010 Radix (25 分)(二分)

    题意: 输入两个数可能包含小写字母,1或者2,进制大小.第三个数为代表第一个数是第四个数进制的,求第二个数等于第一个数时进制的大小,不可能则输出Impossible,第三个数为2代表第二个数是第四个数 ...

  4. [经验] SpringBoot 远程连接 Linux 上的 Redis

    开发环境: ---------- springboot 2.X ---------- Linux Ubuntu 18.0.04 关于怎么在 Ubuntu 上安装 Linux , 网上的教程一大堆, 这 ...

  5. MD5摘要

    MD5简介 MD5即Message-Digest Algorithm 5(信息-摘要算法),属于摘要算法,是一个不可逆过程,就是无论多大数据,经过算法运算后都是生成固定长度的数据,结果使用16进制进行 ...

  6. uniGUI之新窗口uniForm(19)

    然后 保存,在这里 重命名窗口 //主窗口 调用 // NewForm2.UniForm1.Show() ; //非阻塞 NewForm2.UniForm1.ShowModal();//阻塞 //子窗 ...

  7. nginx_2_nginx进程模型

    1.nginx进程模型概述 在上一节我们已经已经成功在linux服务器上安装了nginx,启动nginx后,查看进程:ps -ef | grep nginx 能看到启动nginx进程后,有一个mast ...

  8. 笔记||Pyhthon3进阶之多线程操作共享数据

    # 多线程操作共享数据--------------------------------------------------------------- # import threading# 使用锁# ...

  9. 复习jquery菜鸟教程

    https://www.runoob.com/jquery/jquery-plugin-treeview.html

  10. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 表格:表示成功的操作

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...