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. Python相关资料收集

    读写Excel: http://blog.csdn.net/five3/article/details/7034826http://tech.ddvip.com/2012-10/13515777031 ...

  2. 【bzoj3796】Mushroom追妹纸

    Portal -->bzoj3796 Description 给出字符串s1.s2.s3,找出一个字符串w,满足: 1.w是s1的子串: 2.w是s2的子串: 3.s3不是w的子串. ​ 求w的 ...

  3. 驱动之NandFlash的介绍与应用20170209

    本文主要介绍的是NAND FLASH的介绍与应用,直接看个人笔记即可:

  4. Ubuntu 使用Compiz配置炫酷3D桌面

    先看一下效果 要实现这种3D 的效果其实很简单. Step 1:安装N卡驱动工具 sudo apt- 这个东西其实没有太大的作用. Step 2:安装Compiz sudo apt-get insta ...

  5. python之numpy矩阵库的使用(续)

    本文是对我原先写的python常用序列list.tuples及矩阵库numpy的使用中的numpy矩阵库的使用的补充.结合我个人现在对线性代数的复习进度来不断更博. Section 1:行列式的计算 ...

  6. 「Django」rest_framework学习系列-渲染器

    渲染器:作用于页面,JSONRenderer只是JSON格式,BrowsableAPIRenderer有页面,.AdminRenderer页面以admin形式呈现(需要在请求地址后缀添加?fromat ...

  7. excel自带频率分析

    sklearn实战-乳腺癌细胞数据挖掘(博客主亲自录制视频教程) https://study.163.com/course/introduction.htm?courseId=1005269003&a ...

  8. turn服务部署

    centos7.2 git clone https://github.com/coturn/coturnyum -y install openssl-develyum install openssl ...

  9. elasticsearch ik中文分词器的安装配置使用

    安装步骤  https://github.com/medcl/elasticsearch-analysis-ik 以插件形式安装: [elsearch@localhost elasticsearch- ...

  10. LintCode 156: Merge Interval

    LintCode 156: Merge Interval 题目描述 给出若干闭合区间,合并所有重叠的部分. 样例 给出的区间列表 => 合并后的区间列表: [ [ [1, 3], [1, 6], ...