Catenyms
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10186   Accepted: 2650

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
*** 先判断是否存在欧拉路径,然后再按照字典序输出欧拉路径,想写个非递归的深搜,可惜失败了
 #include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<algorithm>
#include<climits>
#define MAXE 1010
#define MAXP 28
using namespace std;
struct Edge
{
int s,t,next;
char str[];
}edge[MAXE];
int head[MAXE];
int degree[MAXP];
int fa[MAXP];
int stack[MAXE];
bool used[MAXP];
bool sign[MAXE];
bool cur[MAXE][MAXE];
int start;
int n;
int top;
bool cmp(Edge a,Edge b)
{
return strcmp(a.str,b.str)>;
}
void add(int s,int t,int ent)
{
edge[ent].s=s;
edge[ent].t=t;
edge[ent].next=head[s];
head[s]=ent;
}
int find(int x)
{
int temp=x,i;
while(fa[x]!=x)
x=fa[x];
while(fa[temp]!=x)
{
i=fa[temp];
fa[temp]=x;
temp=i;
}
return x;
}
bool oula()
{
int temp=,temp2=;
start=edge[n].s;
for(int i=;i<=;i++)
{
if(used[i])
{
if(fa[i]==i)temp++;
if(degree[i])
{
if(degree[i]>||degree[i]<-)return false;
if(degree[i]==-)start=i;
temp2++;
}
}
}
if(temp!=)return false;
if(temp2&&temp2!=)return false;
return true;
}
void dfs(int s)
{
for(int i=head[s];i!=-;i=edge[i].next)
{
if(!sign[i])
{
sign[i]=true;
dfs(edge[i].t);
stack[++top]=i;
}
}
/*while(1)
{
if(top==n)break;
int temp=head[s];
for(int i=head[s];i!=-1;temp=i=edge[i].next)
{
if(!sign[i]&&!cur[stack[top]][i])break;
}
if(temp==-1)
{
cur[stack[top-1]][stack[top]]=true;
sign[stack[top]]=false;
sign[stack[top-1]]=false;
s=edge[stack[--top]].s;
}
else
{
stack[++top]=temp;
s=edge[temp].t;
sign[temp]=true;
}
}*/
}
int main()
{
int cas;
scanf("%d",&cas);
while(cas--)
{
memset(head,-,sizeof(head));
memset(sign,false,sizeof(sign));
memset(used,false,sizeof(used));
memset(cur,false,sizeof(cur));
memset(degree,,sizeof(degree));
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%s",edge[i].str);
sort(edge+,edge++n,cmp);
for(int i=;i<=;i++)
fa[i]=i;
for(int i=;i<=n;i++)
{
int s=edge[i].str[]-'a'+,t=edge[i].str[strlen(edge[i].str)-]-'a'+;
add(s,t,i);
fa[find(t)]=find(s);
degree[s]--;
degree[t]++;
used[s]=true;
used[t]=true;
}
if(!oula())
{
printf("***\n");
}
else
{
top=;
dfs(start);
/*for(int i=1;i<=top-1;i++)
printf("%s.",edge[stack[i]].str);
printf("%s\n",edge[stack[top]].str);*/
for(int i=top;i>=;i--)
printf("%s.",edge[stack[i]].str);
printf("%s\n",edge[stack[]].str);
}
}
return ;
}

poj 2337 有向图输出欧拉路径的更多相关文章

  1. poj 2337 Catenyms 【欧拉路径】

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

  2. poj 2337 欧拉回路输出最小字典序路径 ***

    把26个小写字母当成点,每个单词就是一条边. 然后就是求欧拉路径. #include<cstdio> #include<iostream> #include<algori ...

  3. POJ 2337 输出欧拉路径

    太无语了. 这道题做了一整天. 主要还是我太弱了. 以后这个就当输出欧拉路径的模版吧. 题目中的输出字典序最小我有点搞不清楚,看了别人是这么写的.但是我发现我过不了后面DISCUSS里面的数据. 题意 ...

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

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

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

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

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

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

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

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

  8. poj 2337(单向欧拉路的判断以及输出)

    Catenyms Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11648   Accepted: 3036 Descrip ...

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

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

随机推荐

  1. strcat函数的使用需要注意的问题

    曾被这个函数困扰了好久,然后各种假设,验证:但是最后却发现这个函数并没有什么好讲的,原来的过错一切都源于忽略了“*dst去掉\0,然后加上*src,最后返回*dst”这句话的真正含义:给*dst分配的 ...

  2. 生成new, old的 shell script

    #!/bin/bash #usage: ./create_dts_diff_v2.x.sh path1 path2 __new_dir=$1 __old_dir=$2 #=============== ...

  3. spring boot Swagger 集成

    1. pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://ww ...

  4. 高度平衡的二叉搜索树(AVL树)

    AVL树的基本概念 AVL树是一种高度平衡的(height balanced)二叉搜索树:对每一个结点x,x的左子树与右子树的高度差(平衡因子)至多为1. 有人也许要问:为什么要有AVL树呢?它有什么 ...

  5. TOMCAT如何建立两个端口或服务

    近日,一个客户需要将系统放到公网上,局网测试的时候用的8080,但该端口已经被其它应用占用,但又不想更改之前的端口,于是查了下资料,以供后阅 针对客户的这个情况,只是说想增加一个端口,这时只需要去to ...

  6. HackerRank "Kruskal (MST): Really Special Subtree"

    Kruskal Algorithm is based on Union-Find - quite intuitive. #include <vector> #include <ios ...

  7. pg强制删库

    在某些时候,由于有别的连接,无法删除数据库,这时候用这个 SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity ...

  8. 一篇关于SpringMVC 传统文件上传的方法

    一.界面效果 二.html代码 <legend>上传APK文件</legend> <form action="<%=basePath%>/apks/ ...

  9. Java中的异常-Throwable-Error-Exception-RuntimeExcetpion-throw-throws-try catch

    今天在做一个将String转换为Integer的功能时,发现Integer.parseInte()会抛出异常NumberFormatException. 函数Integer.parseInt(Stri ...

  10. SG函数闲扯(转)

    http://ydcydcy1.blog.163.com/blog/static/216089040201342412717440/ 没来得及看.