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. CheckBoxList 用法

    <asp:CheckBoxList ID="cblqf" ForeColor="#4d6fc8" runat="server" Rep ...

  2. C++ Primer : 第十三章 : 动态内存管理类

    /* StrVec.h */ #ifndef _STRVEC_H_ #define _STRVEC_H_ #include <memory> #include <string> ...

  3. 【转载】IIS7.5(经典模式)访问静态资源(.css和.js文件)提示:未能执行 URL

    IIS7.5(经典模式)静态资源(.css和.js文件)提示:未能执行 URL “/”应用程序中的服务器错误. 未能执行 URL. 说明: 执行当前 Web 请求期间,出现未处理的异常.请检查堆栈跟踪 ...

  4. Maven学习 (一) 搭建Maven环境

      有两种方式可以配置maven的环境配置,本人推荐使用第二种,即使用本地的maven安装文件,个人感觉这样可以方便管理下载jar包的存放位置,错误信息的输出等,可以在dos窗口中可以清晰看到,虽然比 ...

  5. SoftEnther VPN 在Window的使用

    1.首先下载SoftEnther VPN Client 下载地址 2. 下载后,执行vpngate-client-×××.exe 文件 选择安装一个软件部分: SoftEnther VPN Clien ...

  6. knockout 学习实例6 attr

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  7. php接口和抽象类

    接口关键字:interface,不加class关键字接口里面有成员方法,但是没有函数体.实现接口使用的关键字:implements 不是extends子类必须实现接口的所有方法 使用接口,你可以指定某 ...

  8. JAVA监听器原理

    http://blog.csdn.net/longyulu/article/details/25054697 JAVA监听器原理 标签: 监听器 2014-05-05 15:40 9070人阅读 评论 ...

  9. C#:安装Windows服务,动态指定服务名及描述

    Installer.cs>> public Installer() { InitializeComponent(); /* 服务未注册前,System.Configuration.Conf ...

  10. node.js和express.js安装和使用步骤 [windows]

    PS: NODEJS:https://nodejs.org NPM:https://www.npmjs.com/ 一.node.js安装与配置 到https://nodejs.org/en/downl ...