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

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

 
 
 
 

把26个小写字母当成点,每个单词就是一条边。

然后就是求欧拉路径。

为了保证字典序最小,要先排序,加边要按照顺序加。

而且求解的dfs起点要选择下,选择最小的。

 /* ***********************************************
Author :kuangbin
Created Time :2014-2-3 13:12:43
File Name :E:\2014ACM\专题学习\图论\欧拉路\有向图\POJ2337.cpp
************************************************ */ #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
struct Edge
{
int to,next;
int index;
bool flag;
}edge[];
int head[],tot;
void init()
{
tot = ;
memset(head,-,sizeof(head));
}
void addedge(int u,int v,int index)
{
edge[tot].to = v;
edge[tot].next = head[u];
edge[tot].index = index;
edge[tot].flag = false;
head[u] = tot++;
}
string str[];
int in[],out[];
int cnt;
int ans[];
void dfs(int u)
{
for(int i = head[u] ;i != -;i = edge[i].next)
if(!edge[i].flag)
{
edge[i].flag = true;
dfs(edge[i].to);
ans[cnt++] = edge[i].index;
}
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int T,n;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i = ;i < n;i++)
cin>>str[i];
sort(str,str+n);//要输出字典序最小的解,先按照字典序排序
init();
memset(in,,sizeof(in));
memset(out,,sizeof(out));
int start = ;
for(int i = n-;i >= ;i--)//字典序大的先加入
{
int u = str[i][] - 'a';
int v = str[i][str[i].length() - ] - 'a';
addedge(u,v,i);
out[u]++;
in[v]++;
if(u < start)start = u;
if(v < start)start = v;
}
int cc1 = , cc2 = ;
for(int i = ;i < ;i++)
{
if(out[i] - in[i] == )
{
cc1++;
start = i;//如果有一个出度比入度大1的点,就从这个点出发,否则从最小的点出发
}
else if(out[i] - in[i] == -)
cc2++;
else if(out[i] - in[i] != )
cc1 = ;
}
if(! ( (cc1 == && cc2 == ) || (cc1 == && cc2 == ) ))
{
printf("***\n");
continue;
}
cnt = ;
dfs(start);
if(cnt != n)//判断是否连通
{
printf("***\n");
continue;
}
for(int i = cnt-; i >= ;i--)
{
cout<<str[ans[i]];
if(i > )printf(".");
else printf("\n");
}
}
return ;
}

POJ 2337 Catenyms (有向图欧拉路径,求字典序最小的解)的更多相关文章

  1. poj 2337 Catenyms 【欧拉路径】

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

  2. HDU 5915 The Fastest Runner Ms. Zhang (CCPC2016 长春 E题,分类讨论 + 求字典序最小的直径 + 数据结构寻找最小值)

    题目链接  CCPC2016 Changchun Problem E 题意  给定一个$n$个点$n$条边的无向图,现在从某一点$s$出发,每个点都经过一遍,最后在$t$点停止,经过的边数为$l$   ...

  3. [poj2337]求字典序最小欧拉回路

    注意:找出一条欧拉回路,与判定这个图能不能一笔联通...是不同的概念 c++奇怪的编译规则...生不如死啊... string怎么用啊...cincout来救? 可以直接.length()我也是长见识 ...

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

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

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

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

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

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

  7. POJ 2337 Catenyms

    http://poj.org/problem?id=2337 题意: 判断给出的单词能否首尾相连,输出字典序最小的欧拉路径. 思路: 因为要按字典序大小输出路径,所以先将字符串排序,这样加边的时候就会 ...

  8. POJ 2337 Catenyms (欧拉图)

    本文链接http://i.cnblogs.com/EditPosts.aspx?postid=5402042 题意: 给你N个单词,让你把这些单词排成一个序列,使得每个单词的第一个字母和上一个字单词的 ...

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

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

随机推荐

  1. HTTP协议之响应头Date与Age

    HTTP没有为用户提供一种手段来区分响应是缓存命中的,还是访问原始服务器得到的.客户端有一种方法能判断响应是否来自缓存,就是使用Date首部.将响应中Date首部的值与当前时间进行比较,如果响应中的日 ...

  2. [Alg::Trick]小白鼠找毒酒

    题目来源:牛客网 https://www.nowcoder.com/questionTerminal/c26c4e43c77440ee9497b20118871bf1 8瓶酒一瓶有毒,用人测试.每次测 ...

  3. java中final、finally、finalized使用方法

    首先需要明白  final和finally是关键字,finalize是一个方法. 1. final关键字 final可以修饰类.方法.变量, 修饰类表示类不可以被继承 修饰方法表示此方法不可以被重写( ...

  4. python3之安装、pip、setuptools

    1.python3安装 下载地址:https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tgz #安装环境centOS 7 #安装依赖包: yum ...

  5. python之pip安装mysql-python失败

    前言 由于公司使用的python版本是python2,并且连接mysql的包是mysql-python,但是mysql-python 使用pip安装报错,需要C++环境等依赖,于是使用wheel直接安 ...

  6. BAT获取FTP指定文件

    以下两个文件放在同一目录下 getfile.bat文件内容如下: @echo offftp.exe -i -s:getfile.txt 192.168.1.2(更换成你的ip,参数之间有空格)paus ...

  7. win7下PHP+MySQL+CoreSeek中文检索引擎配置

    1.Windows下的coreseek安装测试 (64位win7旗舰版) 官方参考:http://www.coreseek.cn/products-install/install_on_windows ...

  8. php毫秒时间戳

    2014年5月23日 17:46:04 凡事还是得靠自己呀,网上太多坑 如果你的机器是 32位 的可以看这个: list($usec, $sec) = explode(' ', microtime() ...

  9. python中使用pillow绘制汉字

    # pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pillow from PIL import Image, ImageFont, I ...

  10. PHP内置函数实现简单洗牌

    function wash_card($num_card) { $a = array_keys(array_fill(0, $num_card, '')); $b = array_keys(array ...