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. 【省选水题集Day1】一起来AK水题吧! 题解(更新到B)

    题目:http://www.cnblogs.com/ljc20020730/p/6937936.html 水题A:[AHOI2001]质数和分解 安徽省选OI原题!简单Dp. 一看就是完全背包求方案数 ...

  2. java发送邮件功能[转]

    原文链接:https://blog.csdn.net/jjkang_/article/details/56521959 Javamail遵循两个协议,一个是smtp协议,另一个是pop3协议.一般情况 ...

  3. hihocoder 1580 dp最大子矩阵和

    题意: 给出n*m的矩阵求最大子矩阵和,要求必须把矩阵中的某一个元素替换成p 代码: //求最大子矩阵和,容易想到压缩之后dp但是这道题要求必须替换一次p,必然优先替换最小的. //这样如果求得的结果 ...

  4. python基础6--目录结构

    为什么要设计好目录结构? "设计项目目录结构",就和"代码编码风格"一样,属于个人风格问题.对于这种风格上的规范,一直都存在两种态度: 一类同学认为,这种个人风 ...

  5. java-压缩文件成zip文件(多文件/单文件/多目录/单目录/无目录),用于下载

    本博客是自己在学习和工作途中的积累与总结,仅供自己参考,也欢迎大家转载,转载时请注明出处. http://www.cnblogs.com/king-xg/p/6424788.html 上代码: pac ...

  6. Ubuntu 火狐浏览器中,鼠标选择文字被删除的解决办法

    copy from :http://blog.csdn.net/shadow066/article/details/50628019 在终端中输入命令:ibus-setup 将 “在应用程序窗口中启用 ...

  7. uva 557 Burger

    https://vjudge.net/problem/UVA-557 题意: n个人,n/2个牛肉煲,n/2个鸡肉堡 每次抛硬币,根据正反决定每个人吃什么汉堡 如果某一个汉堡被选完了,就不抛了 问最后 ...

  8. 边双连通缩点+树dp 2015 ACM Arabella Collegiate Programming Contest的Gym - 100676H

    http://codeforces.com/gym/100676/attachments 题目大意: 有n个城市,有m条路,每条路都有边长,如果某几个城市的路能组成一个环,那么在环中的这些城市就有传送 ...

  9. HDU 5700 优先队列(或者multiset) 或 线段树

    题目大意:有n个区间,求k个区间,使得这k个区间相交的区间内数字之和最大.数列的数字均>=0 优先队列思路: 按照左端点sort,然后枚举左端点,假设他被覆盖过k次,然后用优先队列来维护最右端即 ...

  10. Python学习笔记(四十)— 内置模块(9)HTMLParser

    摘抄自:https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/001432002312 ...