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. (转)Unity3D研究院之手游开发中所有特殊的文件夹(assetbundle与Application.persistentDataPath)

    这里列举出手游开发中用到了所有特殊文件夹. 1.Editor Editor文件夹可以在根目录下,也可以在子目录里,只要名子叫Editor就可以.比如目录:/xxx/xxx/Editor  和 /Edi ...

  2. 很有用的mobile web application远程调试工具 weinre

    在移动web应用中,因为没有类似chrome和firebug的调试工具,调试起来比在PC上相对麻烦一些,有时候仅仅能重复进行改动比对,但使用weinre我们能够轻松做到远程调试的功能. 什么是wein ...

  3. IIS 7.5: HOW TO ENABLE TLS 1.1 AND TLS 1.2

    In IIS 7.5, which is installed on Windows 2008 R2 servers, only SSL 3.0 and TLS 1.0 are enabled for ...

  4. LogCat用法2

    如果adb devices显示设备的状态是offline, 这是可以看手机的通知栏,会有一个"触摸以设置USB"这样一个类似的提示,触摸它以后会要求你选择USB的用途,选择第二个' ...

  5. 【Python】安装geocoder

    C:\Users\horn1\Desktop\python\49-geo>pip install geocoder Collecting geocoder Downloading https:/ ...

  6. ZH奶酪:使用PHP调用REST API

    原文:http://yuguo.us/weblog/php-rest-api/ 表征状态转移(英文:REpresentational State Transfer,简称REST)是Roy Fieldi ...

  7. Linux下安装Supervisor的多种方法

    一.安装 1.方法一: pip install  supervisor #!/bin/bash wget http://pypi.python.org/packages/source/s/setupt ...

  8. Linux中awk命令的简单用法

    一.用例1: cat /proc/meminfo|grep "MemTotal"|awk '{print $2}' 说明,$2表示第2位,$0表示全部,如需表示$,可用$$转义.

  9. SqlServer2012自增主键跳跃增长的问题解决方案

    1.问题:SqlServer2012自增主键插入几条数据,然后重启服务,然后再插入几条数据,发现重启后插入的记录ID出现跳跃. 2.解决方案: Open SQLServer configuration ...

  10. websocket与canvas[转]

    server端还是用tomcat7的方式客户端 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  ...