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. 【转】BFC是什么

    原文:https://www.cnblogs.com/mlw1814011067/p/10397999.html ------------------------------------------- ...

  2. (C++)字符串分割

    题目: 如何对C++中输入的字符串进行分割呢?如“I am a student”,去除空格后分割成为“I”,“am”, “a”, “student”四个单词 思路: 直接参考代码 代码: void s ...

  3. Oracle ODP.NET vs Microsoft MSDP

    08年做项目时,用 VS 2005,写据库层还在用 MSDP(System.Data.OracleClient),但是当时由于要操作 XML 和二进制文件,所以又使用了 ODP.NET(Oracle. ...

  4. input[type="checkbox"]与label对齐

    项目中遇到文字与 checkbook 无法水平对齐, 源码如下: <div align='center'> <input type="checkbox" id=& ...

  5. 【linux】Ubuntu中shell脚本无法使用source的原因及解决方法

    问题现象: shell脚本中source aaa.sh时提示 source: not found 原因: ls -l `which sh` 提示/bin/sh -> dash 这说明是用dash ...

  6. HDS Truecopy实现原理及项目的选择-诸多案例

    copy from:http://www.eygle.com/archives/2009/05/hds_truecopy_dataguard.html 诸多案例:http://wenku.baidu. ...

  7. Vue router 一个路由对应多个视图

    使用命名路由 https://jsfiddle.net/posva/6du90epg/ <script src="https://unpkg.com/vue/dist/vue.js&q ...

  8. bat脚本禁用和开启本地连接

    netsh interface set interface name="本地连接" admin=disabled //禁用本地连接 netsh interface set inte ...

  9. 架构师速成7.3-devops为什么非常重要

    evops是一个非常高大上的名字,事实上说的简单点就是开发和运维本身就是一个团队的,要干就一起把事情干好.谁出了问题,站点都不行. 作为一个架构师.必需要devops,并且要知道怎样推行devops. ...

  10. Java中的List

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6538256.html Java中常用的List子类主要有:ArrayList.LinkedList.Vecto ...