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

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

题目大意:输入n个单词,每个单词都形成一条从该单词首字母到尾字母的边,单词尾字母要与下一个单词首字母相同,若可以组成这样的路,即可以组成这样一条连着的单词串,输出路径(单词串),若有多条,则要按字典顺序输出,找不到路则输出***。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm> using namespace std; int n,cnt,head[];
int tot,odd,src,des,mark[],indeg[],outdeg[],deg[],father[];
char res[][]; struct Edge{
bool vis;
char wrd[];
int to,nxt;
}edge[]; int cmp(Edge a,Edge b){
return strcmp(a.wrd,b.wrd)>;
} void init(){
tot=odd=;
memset(mark,,sizeof(mark));
memset(indeg,,sizeof(indeg));
memset(outdeg,,sizeof(outdeg));
memset(head,-,sizeof(head));
for(int i=;i<;i++)
edge[i].vis=;
for(int i=;i<;i++)
father[i]=i;
} int findSet(int x){
if(x!=father[x]){
father[x]=findSet(father[x]);
}
return father[x];
} int judge(){ //判断是否满足欧拉。0不满足,1欧拉回路,2欧拉路
int i,k;
for(i=;i<;i++){ //判断有向图欧拉
if(!mark[i])
continue;
deg[i]=indeg[i]-outdeg[i];
if(abs(deg[i])>)
return ;
if(deg[i]>) src=i; //起点
if(deg[i]<) des=i; //终点
if(deg[i]%) odd++;
if(odd>) return ;
}
for(i=;i<;i++)
if(mark[i])
break;
k=findSet(i);
for(i=k+;i<;i++){ //判断连通性
if(!mark[i])
continue;
if(k!=findSet(i))
return ;
}
if(odd==){ //有欧拉回路
for(i=;i<;i++)
if(mark[i])
break;
src=i;
return ;
}
return ;
} void DFS(int u,int id){ //深搜寻找欧拉路径,
for(int i=head[u];i!=-;i=edge[i].nxt)
if(!edge[i].vis){
edge[i].vis=;
DFS(edge[i].to,i);
}
if(id!=-)
strcpy(res[tot++],edge[id].wrd); //最先进去的肯定是终点
} int main(){ //freopen("input.txt","r",stdin); int t;
scanf("%d",&t);
while(t--){
init();
scanf("%d",&n);
for(int i=;i<n;i++)
scanf("%s",edge[i].wrd);
sort(edge,edge+n,cmp); //题目要求是字典顺序,但是我是用前插链表,这时的顺序恰好会相反 ,所以排序时从大到小,这样刚刚会是字典顺序
for(int i=;i<n;i++){
int len=strlen(edge[i].wrd);
int x=edge[i].wrd[]-'a';
int y=edge[i].wrd[len-]-'a';
indeg[x]++; outdeg[y]++;
mark[x]=true; mark[y]=true; edge[i].to=y; edge[i].nxt=head[x]; head[x]=i; //建边 int fx=findSet(x);
int fy=findSet(y);
if(fx!=fy)
father[fx]=fy;
}
if(judge()==){
printf("***\n");
continue;
}
DFS(src,-);
for(int i=tot-;i>;i--)
printf("%s.",res[i]);
printf("%s\n",res[]);
}
return ;
}

POJ 2337 Catenyms (欧拉回路)的更多相关文章

  1. POJ 2337 Catenyms

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

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

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

  3. poj 2337 Catenyms 【欧拉路径】

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

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

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

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

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

  6. POJ 2337 Catenyms (欧拉图)

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

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

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

  8. Poj 2337 Catenyms(有向图DFS求欧拉通路)

    题意: 给定n个单词, 问是否存在一条欧拉通路(如acm,matal,lack), 如果存在, 输出字典序最小的一条. 分析: 这题可以看作http://www.cnblogs.com/Jadon97 ...

  9. poj 2337 && zoj 1919 欧拉回路+连通性判断

    题目要求按字典序排列,而且可能有重边 所以一开始就将数组从大到小排列,那么我将字符串加入链表时就会令小的不断前移,大的被挤到后面 这里有一点问题就是我一开始使用的是qsort: int cmp(con ...

随机推荐

  1. Kafka:ZK+Kafka+Spark Streaming集群环境搭建(二十八):kafka0.10.1 内置性能测试API用法示例

    消费者测试: ./kafka-consumer-perf-test..com.cn:,vm10..com.cn:,vm10..com.cn: --group test-teg1 --messages ...

  2. JPA(七):映射关联关系------映射双向多对一的关联关系

    映射双向多对一的关联关系 修改Customer.java package com.dx.jpa.singlemanytoone; import java.util.Date; import java. ...

  3. awk学习 (good)

    原文:http://blog.chinaunix.net/uid-23302288-id-3785105.html awk是行处理器: 相比较屏幕处理的优点,在处理庞大文件时不会出现内存溢出或是处理缓 ...

  4. USACO Arithmetic Progressions(暴力)

    题目请点我 题解: 这道题的题意是找出集合里全部固定长度为N的等差数列.集合内的元素均为P^2+q^2的形式(0<=p,q<=M).时间要求5s内.本着KISS,直接暴力. 可是后来竟超时 ...

  5. 浅谈压缩感知(十四):傅里叶矩阵与小波变换矩阵的MATLAB实现

    主要内容: 傅里叶矩阵及其MATLAB实现 小波变换矩阵及其MATLAB实现  傅里叶矩阵及其MATLAB实现 傅里叶矩阵的定义:(来源: http://mathworld.wolfram.com/F ...

  6. cocos2d-x 手游源代码站

    尊重开发人员的劳动成果,转载的时候请务必注明出处:http://blog.csdn.net/haomengzhu/article/details/37829061 1.魔幻方块 链接:魔幻方块源代码 ...

  7. 保存登录plsql developer 的用户名和密码

    1 保存用户名 tools -> Preferences -> User Interface - Options 勾选 Autosave username . 保存 2 保存密码 tool ...

  8. TCP/IP, UDP, ICMP, ARP协议族简介--纯图慎点

    ISO/OSI的网络模型架构 TCP/IP参考模型的层次结果 以太网头部结构 以太网属于数据链路层, 属于最基本的协议结构 IP协议 IP协议为TCP, UDP, ICMP提供最基本的数据传输通路 I ...

  9. 微信小程序 - 3d轮播图组件(基础)

    <!-- 目前仅支持data数据源来自banner,请看测试案例 ################ 以上三种形式轮播: 1. basic 2. 3d 3. book basic即普通轮播 3d即 ...

  10. 第六周 Word目录和索引

    第六周 Word目录和索引 教学时间 2013-4-2 教学课时 2 教案序号 5 教学目标 能正确使用索引.目录等 教学过程: 复习提问 1.脚注和尾注的区别是什么?2.如何插入脚注和尾注?3.如何 ...