Catenyms
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 11526   Accepted: 2993

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 <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <vector>
#include<functional>
#define mod 1000000007
#define inf 0x3f3f3f3f
#define pi acos(-1.0)
using namespace std;
typedef long long ll;
const int N=;
const int M=;
ll power(ll a,int b,ll c) {
ll ans=;
while(b) {
if(b%==) {
ans=(ans*a)%c;
b--;
}
b/=;
a=a*a%c;
}
return ans;
}
struct Word { int l; char s[]; };
struct Edge { int st, ed; bool del; }; Word word[];
Edge edge[];
int in[], out[];
int stk[], father[];
bool mark[];
int E, top; int cmp ( const void* a, const void* b )
{
return strcmp( ((Word*)a)->s, ((Word*)b)->s );
} int find_set ( int x )
{
if ( father[x] != x )
father[x] = find_set ( father[x] );
return father[x];
} bool judge ()
{
int t = ;
for ( int i = ; i < ; i++ )
if ( mark[i] && father[i] == i ) t++;
return t == ;
} void find_path ( int u )
{
for ( int i = ; i < E; i++ )
{
if ( ! edge[i].del && edge[i].st == u )
{
edge[i].del = true;
find_path ( edge[i].ed );
stk[top++] = i;
}
}
} int main()
{
int cs;
scanf("%d",&cs);
while ( cs-- )
{
scanf("%d",&E);
int u, v, c1, c2, start, i; for ( i = ; i < ; i++ )
{
in[i] = out[i] = ;
father[i] = i;
mark[i] = false;
} for ( i = ; i < E; i++ )
{
scanf("%s",word[i].s);
word[i].l = strlen(word[i].s);
} qsort(word, E, sizeof(word[]), cmp); for ( i = ; i < E; i++ )
{
u = word[i].s[] - 'a';
v = word[i].s[word[i].l-] - 'a';
edge[i].st = u;
edge[i].ed = v;
edge[i].del = false;
mark[u] = mark[v] = true;
out[u]++; in[v]++;
u = find_set ( u );
v = find_set ( v );
if ( u != v ) father[v] = u;
} c1 = c2 = ;
start = edge[].st;
for ( i = ; i < ; i++ )
{
if ( in[i] == out[i] ) continue;
else if ( in[i] - == out[i] ) c1++;
else if ( out[i] - == in[i] ) { c2++; start = i; }
else break;
} if ( i == && ((c1 == c2 && c1 == ) || (c1 == c2 && c1 == )) && judge() )
{
top = ;
find_path ( start );
for ( i = top - ; i > ; i-- )
printf("%s.",word[stk[i]].s);
printf("%s\n",word[stk[]].s);
}
else printf("***\n");
}
//system("pause");
return ;
}

POJ2337 Catenyms(欧拉通路的求解)的更多相关文章

  1. ACM/ICPC 之 DFS求解欧拉通路路径(POJ2337)

    判断是欧拉通路后,DFS简单剪枝求解字典序最小的欧拉通路路径 //Time:16Ms Memory:228K #include<iostream> #include<cstring& ...

  2. POJ 1300 欧拉通路&欧拉回路

    系统的学习一遍图论!从这篇博客开始! 先介绍一些概念. 无向图: G为连通的无向图,称经过G的每条边一次并且仅一次的路径为欧拉通路. 如果欧拉通路是回路(起点和终点相同),则称此回路为欧拉回路. 具有 ...

  3. poj 2513 连接火柴 字典树+欧拉通路 好题

    Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 27134   Accepted: 7186 ...

  4. poj2513- Colored Sticks 字典树+欧拉通路判断

    题目链接:http://poj.org/problem?id=2513 思路很容易想到就是判断欧拉通路 预处理时用字典树将每个单词和数字对应即可 刚开始在并查集处理的时候出错了 代码: #includ ...

  5. hdu1116有向图判断欧拉通路判断

    Play on Words Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

  6. Colored Sticks POJ - 2513 并查集+欧拉通路+字典树hash

    题意:给出很多很多很多很多个棒子 左右各有颜色(给出的是单词) 相同颜色的可以接在一起,问是否存在一种 方法可以使得所以棒子连在一起 思路:就是一个判欧拉通路的题目,欧拉通路存在:没奇度顶点   或者 ...

  7. 欧拉回路&欧拉通路判断

    欧拉回路:图G,若存在一条路,经过G中每条边有且仅有一次,称这条路为欧拉路,如果存在一条回路经过G每条边有且仅有一次, 称这条回路为欧拉回路.具有欧拉回路的图成为欧拉图. 判断欧拉通路是否存在的方法 ...

  8. POJ2513Colored Sticks(欧拉通路)(字典树)(并查集)

                                                             Colored Sticks Time Limit: 5000MS   Memory ...

  9. HDU 5883 F - The Best Path 欧拉通路 & 欧拉回路

    给定一个图,要求选一个点作为起点,然后经过每条边一次,然后把访问过的点异或起来(访问一次就异或一次),然后求最大值. 首先为什么会有最大值这样的分类?就是因为你开始点选择不同,欧拉回路的结果不同,因为 ...

随机推荐

  1. Jmeter 场景设计

    今天的业务场景是: 1.管理员登录后台---登录成功后添加一个某类型的产品---产品添加成功后,再为该产品添加10个排期. 2.管理员登录后台--登录成功后添加多个不同类型产品---产品全部添加完成后 ...

  2. Java8 时间处理

    Table of Contents 前言 时间单位和时区 时间点 时间段 时间的解析和格式化 时区时间 兼容旧接口 结语 前言 时间处理是一个经常会用到但又不那么好用的功能,其中的主要问题在于对人友好 ...

  3. python解析复杂json字符串

    因为项目需要,公司领导对提出了接口测试的要求,因此作为一个测试人员,我第一时间就想到了jmeter这个利器,前面文章也有说明过怎么用jmeter做http协议的接口测试,这里我不再做讲解,此篇主要讲解 ...

  4. Oracle 插入数据时获取系统时间

    insert into table_xx (xx,dateTime) values( 'xx',sysdate) 这个sysdate 相当于sql server中的GETDATE()函数 另to_ch ...

  5. HDU 4760 Good Firewall ( Trie树 )

    一开始看的时候就想歪了,比赛的时候一直在YY线段树区间覆盖,然后纠结节点数太多开不下怎么办啊啊啊啊…… 然后昨天吃饭的时候也在纠结这到底是个啥题,后来发现公共前缀->前缀??!!!!->这 ...

  6. C#学习笔记----静态字段和静态方法

    1.使用关键字 static 修饰的字段或方法成为静态字段和静态方法,如 public static int num = 1;2.静态字段属于类,并为类所用.而非静态字段属于对象,只能被特定的对象专有 ...

  7. JDK从1.8.x升级到9.0.1后Tomcat 8.0.x不能启动

    目录 描述 具体环境情况 处理办法 描述 JDK在今年9月发布后,我们项目也打算测试升级使用JDK 9.在我将JDK升级成 JDK 9.0.1后,启动tomcat失败(黑框一闪就没了).具体失败信息如 ...

  8. PAT 1050 螺旋矩阵

    https://pintia.cn/problem-sets/994805260223102976/problems/994805275146436608 本题要求将给定的 N 个正整数按非递增的顺序 ...

  9. SQL小助手——SQL Prompt

    背景: 当数据库设计的比较复杂.庞大时,我们如果对脚本不是很熟悉,就会很难完成看似简单的增.删.改.查的操作.我们需要一款软件来给出相应的提示或帮助,来提高代码的可读性,更快更好的完成任务. 简介: ...

  10. [bzoj3218] a+b problem [最小割+数据结构优化建图]

    题面 传送门 思路 最小割 我们首先忽略掉那个奇♂怪的限制,就有一个比较显然的最小割模型: 建立源点$S$和汇点$T$ 对于每个元素$i$建立一个点$i$,连边$<S,i,w[i]>$和$ ...