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. 循环神经网络(RNN, Recurrent Neural Networks)介绍(转载)

    循环神经网络(RNN, Recurrent Neural Networks)介绍    这篇文章很多内容是参考:http://www.wildml.com/2015/09/recurrent-neur ...

  2. Java 报表之JFreeChart(第二讲)

    1.利用 JFreeChart 创建按颜色分类的水果销售报表 package com.wcy.chart.bar; import javax.servlet.http.HttpSession; imp ...

  3. java json-lib.jar

    import java.util.ArrayList; import java.util.Date; import java.util.List; import net.sf.json.JSONObj ...

  4. 不同操作系统上屏蔽oracle的操作系统认证方式

    windows系统上>如果不想用户通过操作系统验证方式登录,可以修改 sqlnet.ora文件,把 SQLNET.AUTHENTICATION_SERVICES=NTS 前面加#注释掉就可以了. ...

  5. C#List转字符串,字符串转List,字符数组转Int数组

    List转字符串 [C#] 纯文本查看 复制代码 ? 01 02 List<string> List = new List<string>(); string strArray ...

  6. Fragment的陷阱(转)

    以前做过的一个项目,Fragment嵌套高德地图,当再次进入Fragment的时候,会出现奇怪的现象.嵌套的地图会出现滑动不动的情况,起先还以为是高德的bug呢,经过一番研究,终确定这是一个坑. 先对 ...

  7. java程序打包成jar文件,使用到第三方jar包

    1.右击工程选择Export—>选择JAR file—>选择NEXT,如下图所示 2.选择需要打包的工程,并且选择存放目录,我这放在 E:\jartest 目录下,然后点击NEXT,如下图 ...

  8. Bootstrap整体架构

    大多数Bootstrap的使用者都认为Bootstrap只是提供了CSS组件和JavaScript插件,其实CSS组件和JavaScript插件只是Bootstrap框架的表现形式而已,他们都是构建在 ...

  9. phonegap(cordova)环境配置

    首先要配置好  java jdk 和 java jre 环境 配置之后 控制台  javac -version 查看是否配置成功 然后配置 Android sdk 配置之后 控制台 输入 adb 查看 ...

  10. MySQL绿色版安装(mysql-5.7.12-win32)

    1. 从官网下载安装包:mysql-5.7.12-win32.zip 2. 解压到C盘(任意盘符都行) C:\mysql-5.7.12-win32 3. 修改配置文件:C:\mysql-5.7.12- ...