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. Java 8 stream 实战

    概述 平时工作用python的机会比较多,习惯了python函数式编程的简洁和优雅.切换到java后,对于数据处理的『冗长代码』还是有点不习惯的.有幸的是,Java8版本后,引入了Lambda表达式和 ...

  2. Git fork后如何同步源仓库更新

    1. 设置源仓库的远程地址 >> git remote add [新地址名称] [源仓库远程地址] >> git remote add upstream https://git ...

  3. 6 JavaScript函数&内置构造&函数提升&函数对象&箭头函数&函数参数&参数的值传递与对象传递

    JavaScript函数:使用关键字function定义,也可以使用内置的JavaScript函数构造器定义 匿名函数: 函数表达式可以存储在变量中,并且该变量也可以作为函数使用. 实际上是匿名函数. ...

  4. Linux软Raid--mdadm命令

    mdadm:为软RAID提供管理界面,RAID设备可命名为/dev/md0./dev/md1./dev/md2./dev/md3等 命令的语法格式:mdadm[mode] <raiddevice ...

  5. 【PAT甲级】1037 Magic Coupon (25 分)

    题意: 输入一个正整数N(<=1e5),接下来输入N个整数.再输入一个正整数M(<=1e5),接下来输入M个整数.每次可以从两组数中各取一个,求最大的两个数的乘积的和. AAAAAccep ...

  6. Linux进程管理(二)

    目录 Linux进程管理(二) 参考 vmstat命令 top命令 Linux进程管理(二)

  7. Day11 - K - Good Luck in CET-4 Everybody! HDU - 1847

    大学英语四级考试就要来临了,你是不是在紧张的复习?也许紧张得连短学期的ACM都没工夫练习了,反正我知道的Kiki和Cici都是如此.当然,作为在考场浸润了十几载的当代大学生,Kiki和Cici更懂得考 ...

  8. 123、Java面向对象之引用传递实例一

    01.代码如下: package TIANPAN; class Message { private int num = 10; // 定义int基本类型的属性 public Message(int n ...

  9. 110、Java中String类之字符串文本拆分

    01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...

  10. 寒假所做事情日志-Office重新激活

    日期:2020.01.18 博客期:127 星期六 好吧,今天出了一趟远门,将近傍晚才回来.任务目标其实相当于什么也没做,但回来发现Office居然过期了,老师给的那些文件居然无法修改了,于是乎剩下的 ...