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. Python列表深浅复制详解

    转自:https://www.cnblogs.com/blaomao/p/7239203.html 在文章<Python 数据类型>里边介绍了列表的用法,其中列表有个 copy() 方法, ...

  2. android之SlideMenu双向滑动

    开始动手之前先来讲一下实现原理,在一个Activity的布局中需要有三部分,一个是左侧菜单的布局,一个是右侧菜单的布局,一个是内容布局.左侧菜单居屏幕左边缘对齐,右侧菜单居屏幕右边缘对齐,然后内容布局 ...

  3. resharper激活

    1.解压后点击64位系统的IntelliJIDEALicenseServer_windows_amd64.exe      32位点击IntelliJIDEALicenseServer_windows ...

  4. 【bzoj2044】三维导弹拦截 dp+二分图最大匹配

    题目描述 n个物品,第i个位置有ai.bi.ci三种属性.每次可以选出满足$\ a_{p_i}<a_{p_{i+1}}\ ,\ b_{p_i}<b_{p_{i+1}}\ ,\ c_{p_i ...

  5. hdu 1426 Sudoku Killer (dfs)

    Sudoku Killer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  6. 算法复习———dijkstra求次短路(poj3255)

    题目: Description Bessie has moved to a small farm and sometimes enjoys returning to visit one of her ...

  7. (转)Ant使用例子

    文章来自:http://www.blogjava.net/feng0801/archive/2014/07/29/416297.html Ant是一个Apache基金会下的跨平台的构件工具,它可以实现 ...

  8. java中的Timer

    一个java中用Timer做的简单定时器小程序. package com.test.lx; import java.util.TimerTask; public class TimeTaskTest ...

  9. 《R语言实战》读书笔记--第一章 R语言介绍

    1.典型的数据分析过程可以总结为一下图形: 注意,在模型建立和验证的过程中,可能需要重新进行数据清理和模型建立. 2.R语言一般用 <- 作为赋值运算符,一般不用 = ,原因待考证.用-> ...

  10. FOJ ——Problem 1759 Super A^B mod C

     Problem 1759 Super A^B mod C Accept: 1368    Submit: 4639Time Limit: 1000 mSec    Memory Limit : 32 ...