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. 【Theory of Generalization】林轩田机器学习基石

    紧接上一讲的Break Point of H.有一个非常intuition的结论,如果break point在k取到了,那么k+1, k+2,... 都是break point. 那么除此之外,我们还 ...

  2. Python 爬取网页中JavaScript动态添加的内容(二)

    使用 selenium + phantomjs 实现 1.准备环境 selenium(一个用于web应用程测试的工具)安装:pip install seleniumphantomjs(是一种无界面的浏 ...

  3. Python处理Sqlite3数据库

    sqlite3比较小众 本章主要通过Python Code表述如何增.查.改.删 sqlite3 DB 一.直接上代码 #!/usr/bin/env python # -*- coding: utf- ...

  4. python练习题及实现--文件处理、date日期

    练习题作者:Vamei 出处:http://www.cnblogs.com/vamei http://www.cnblogs.com/vamei/archive/2012/07/19/2600135. ...

  5. 脚本,替换ipa里面的资源,并重新打包

    今天逯同事说,可以把狂挂传奇打包的项目写成一个脚本,这样就不用担心证书有时不能用的问题了. 然后,像我这么好学的学生,当然要去执行了.(其实,以前他给的建议我都只是听听而已,这次是因为想学点东西了,所 ...

  6. BOZJ 2045:疯狂的馒头(并查集)

    题目大意:有n个馒头排成一排,初始时颜色为0,进行m次染色,第i次将(i*p+q)mod n到(i*q+p)mod n的馒头全部染成颜色i,求最后所有馒头颜色.n<=10^6 m<=10^ ...

  7. vue数组对象修改触发视图更新

    直接修改数组元素是无法触发视图更新的,如 this.array[0] = { name: 'meng', age: 22 } 修改array的length也无法触发视图更新,如 this.array. ...

  8. 【CF Round 429 B. Godsend】

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  9. APIO2017游记

    铁牌选手爆零滚粗记QAQ........ CCF说不让讨论APIO相关内容不过现在应该没事了吧QAQ day0:上午还在学校填清北夏令营的表,下午上火车去北京,晚上颓颓颓...... day1:上午网 ...

  10. SQL触发器(AFTER和INSTEAD OF)

    转自:http://www.cnblogs.com/shepherldeng/archive/2010/06/23/1763766.html 何为触发器:触发器是数据库服务器中发生事件时自动执行的特种 ...