Catenyms
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 11648   Accepted: 3036

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
*** 题意:给定一系列字符串,如果某个字符串的结尾和另外一个字符串的开头相等,那么两个字符串就可以拼接在一起,现在问所有的字符串能否刚好都出现一次?如果存在,输出字典序最小的那个组合. 题解:将字符串看成一条边,然后将其起始字符和结尾字符看成边的两个端点,构造一个有向图,然后就判断这个图是否为欧拉图了.判断单向欧拉图的方法:

有向欧拉通路:起点:出度-入度=1,终点:入度-出度=1,其它点:入度==出度

有向欧拉回路:所有点:入度==出度

还要利用并查集判断一下这个图是否只有一个连通分量.

然后将所有的边按照字典序排序,找到起点,进行DFS(Edge &e = edge[u][i] 这里检查了半天,一点要记得是地址啊...),即可得到答案.

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <vector>
#include <stack>
using namespace std;
const int N = ;
char str[];
struct Edge{
char str[];
int to,del;
}; typedef vector <Edge> vec;
stack <string> ans;
vec edge[N];
int in[N],out[N],father[N];
bool vis[N],mark[];
int n;
void init(){
memset(in,,sizeof(in));
memset(out,,sizeof(out));
memset(vis,false,sizeof(vis));
for(int i=;i<=;i++){
mark[i] = false;
edge[i].clear();
father[i] = i;
}
}
int _find(int x){
return x==father[x]?x:father[x] = _find(father[x]);
} void dfs(int u){
for(int i=;i<edge[u].size();i++){
Edge &e = edge[u][i]; ///这里要取地址
int v = e.to;
if(!e.del){
e.del = ;
dfs(v);
ans.push(e.str);
}
}
}
bool cmp(Edge a,Edge b){
return strcmp(a.str,b.str)<;
}
int main()
{
int tcase;
scanf("%d",&tcase);
while(tcase--){
init();
scanf("%d",&n);
int S = N;
for(int i=;i<=n;i++){
scanf("%s",str);
int s = str[]-'a'+;
int t = str[strlen(str)-]-'a'+;
Edge e;
e.del = ;
strcpy(e.str,str);
e.to = t;
edge[s].push_back(e);
in[t]++;
out[s]++;
mark[t] = mark[s] = ;
int u = _find(s);
int v = _find(t);
if(u!=v) father[u] = v;
S = min(S,min(s,t));
}
int num1 = ,num2 = ,num3 = ,num4 = ;
for(int i=;i<=;i++){
if(mark[i]==&&father[i]==i) num4++;
if(out[i]-in[i]==){
S = i;
num1++;
}
else if(in[i]-out[i]==){
num2++;
}else if(in[i]-out[i]!=){
num3++;
}
if(!edge[i].empty())
sort(edge[i].begin(),edge[i].end(),cmp);
}
if(num3||!(num1==&&num2==)&&!(num1==&&num2==)||num4>){
printf("***\n");
continue;
}
dfs(S);
int flag = ;
while(!ans.empty()){
if(flag) printf(".");
cout<<ans.top();
flag = ;
ans.pop();
}
printf("\n");
}
}

poj 2337(单向欧拉路的判断以及输出)的更多相关文章

  1. POJ 2230 (欧拉路)

    分析: 基础的欧拉路算法,变化在于要求每条边正向和反向各走一遍. 链式前向星构图,只要标记走过的单向边,边找边输出即可. code #include <iostream> #include ...

  2. poj 2337 && zoj 1919 欧拉回路+连通性判断

    题目要求按字典序排列,而且可能有重边 所以一开始就将数组从大到小排列,那么我将字符串加入链表时就会令小的不断前移,大的被挤到后面 这里有一点问题就是我一开始使用的是qsort: int cmp(con ...

  3. 欧拉路&&欧拉回路 概念及其练习

    欧拉路: 如果给定无孤立结点图G,若存在一条路,经过图中每边一次且仅一次,这条路称为欧拉路: 如果给定无孤立结点图G,若存在一条回路,经过图中每边一次且仅一次,那么该回路称为欧拉回路. 存在欧拉回路的 ...

  4. Play on Words(欧拉路)

    http://poj.org/problem?id=1386 题意:给定若干个单词,若前一个的尾字母和后一个单词的首字母相同,则这两个单词可以连接,问是否所有的单词都能连接起来. 思路:欧拉路的判断, ...

  5. UVA - 10129Play on Words(欧拉路)

    UVA - 10129Play on Words Some of the secret doors contain a very interesting word puzzle. The team o ...

  6. POJ 2513 trie树+并查集判断无向图的欧拉路

    生无可恋 查RE查了一个多小时.. 原因是我N define的是250500 应该是500500!!!!!!!!! 身败名裂,已无颜面对众人.. 吐槽完了 我们来说思路... 思路: 判有向图能否形成 ...

  7. POJ 1637 Sightseeing tour (混合图欧拉路判定)

    Sightseeing tour Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6986   Accepted: 2901 ...

  8. poj 1386 Play on Words(有向图欧拉路+并查集)

    题目链接:http://poj.org/problem?id=1386 思路分析:该问题要求判断单词是否能连接成一条直线,转换为图论问题:将单词的首字母和尾字母看做一个点,每个单词描述了一条从首字母指 ...

  9. POJ 2513 - Colored Sticks - [欧拉路][图的连通性][字典树]

    题目链接: http://poj.org/problem?id=2513 http://bailian.openjudge.cn/practice/2513?lang=en_US Time Limit ...

随机推荐

  1. Win10如何搭建FTP服务器以实现快速传输文件

    原文链接地址:http://blog.csdn.net/bai_langtao/article/details/77751447 Win10如何搭建FTP服务器以实现快速传输文件?相信大家在工作或生活 ...

  2. skip-external-locking --mysql配置说明

    MySQL的配置文件my.cnf中默认存在一行skip-external-locking的参数,即“跳过外部锁定”.根据MySQL开发网站的官方解释,External-locking用于多进程条件下为 ...

  3. bzoj2089&2090: [Poi2010]Monotonicity

    双倍经验一眼题... f[i][1/2]表示以i结尾,当前符号应该是</>的最长上升子序列, 用BIT优化转移就好 =的话就不用说了吧= = #include<iostream> ...

  4. cmakelist 定义字符串,替换到脚本中。

    cmake_minimum_required(VERSION 2.6 FATAL_ERROR) cmake_policy(VERSION 2.6) # . Project Name project(s ...

  5. 机器学习算法的Python实现 (1):logistics回归 与 线性判别分析(LDA)

    先收藏............ 本文为笔者在学习周志华老师的机器学习教材后,写的课后习题的的编程题.之前放在答案的博文中,现在重新进行整理,将需要实现代码的部分单独拿出来,慢慢积累.希望能写一个机器学 ...

  6. HDU 3480 斜率dp

    Division Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 999999/400000 K (Java/Others)Total ...

  7. 2017 Multi-University Training Contest - 1

    hdu 6033 pragma comment(linker, "/STACK:102400000,102400000") #include <cstdio> #inc ...

  8. 关于GCD的几个结论

    设a和b的最大公约数是d,那么: 1. d是用sa+tb(s和t都是整数)能够表示的最小正整数 证明:设x=sa+tb是sa+tb能够表示出的最小正整数.首先,有d|x,证明如下: 因此有x>= ...

  9. jetbrains phpstorm插件开发环境搭建

    2018.04.14 重要更新: 使用 gradle 进行构建可以免去下面大部分步骤,使用 gradle 我们仅需下载安装 JDK.Idea. 使用 gradle 的方法是,新建 Project,然后 ...

  10. Splay 区间操作(二)

    首先基本操作如下: 删除第rank个点 void Remove(int id){//删除第rank个点 rank++; int x = find(root, rank - 1); splay(x, 0 ...