POJ 2337 Catenyms (欧拉回路)
|
Catenyms
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 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 Sample Output aloha.arachnid.dog.gopher.rat.tiger Source |
题目大意:输入n个单词,每个单词都形成一条从该单词首字母到尾字母的边,单词尾字母要与下一个单词首字母相同,若可以组成这样的路,即可以组成这样一条连着的单词串,输出路径(单词串),若有多条,则要按字典顺序输出,找不到路则输出***。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm> using namespace std; int n,cnt,head[];
int tot,odd,src,des,mark[],indeg[],outdeg[],deg[],father[];
char res[][]; struct Edge{
bool vis;
char wrd[];
int to,nxt;
}edge[]; int cmp(Edge a,Edge b){
return strcmp(a.wrd,b.wrd)>;
} void init(){
tot=odd=;
memset(mark,,sizeof(mark));
memset(indeg,,sizeof(indeg));
memset(outdeg,,sizeof(outdeg));
memset(head,-,sizeof(head));
for(int i=;i<;i++)
edge[i].vis=;
for(int i=;i<;i++)
father[i]=i;
} int findSet(int x){
if(x!=father[x]){
father[x]=findSet(father[x]);
}
return father[x];
} int judge(){ //判断是否满足欧拉。0不满足,1欧拉回路,2欧拉路
int i,k;
for(i=;i<;i++){ //判断有向图欧拉
if(!mark[i])
continue;
deg[i]=indeg[i]-outdeg[i];
if(abs(deg[i])>)
return ;
if(deg[i]>) src=i; //起点
if(deg[i]<) des=i; //终点
if(deg[i]%) odd++;
if(odd>) return ;
}
for(i=;i<;i++)
if(mark[i])
break;
k=findSet(i);
for(i=k+;i<;i++){ //判断连通性
if(!mark[i])
continue;
if(k!=findSet(i))
return ;
}
if(odd==){ //有欧拉回路
for(i=;i<;i++)
if(mark[i])
break;
src=i;
return ;
}
return ;
} void DFS(int u,int id){ //深搜寻找欧拉路径,
for(int i=head[u];i!=-;i=edge[i].nxt)
if(!edge[i].vis){
edge[i].vis=;
DFS(edge[i].to,i);
}
if(id!=-)
strcpy(res[tot++],edge[id].wrd); //最先进去的肯定是终点
} int main(){ //freopen("input.txt","r",stdin); int t;
scanf("%d",&t);
while(t--){
init();
scanf("%d",&n);
for(int i=;i<n;i++)
scanf("%s",edge[i].wrd);
sort(edge,edge+n,cmp); //题目要求是字典顺序,但是我是用前插链表,这时的顺序恰好会相反 ,所以排序时从大到小,这样刚刚会是字典顺序
for(int i=;i<n;i++){
int len=strlen(edge[i].wrd);
int x=edge[i].wrd[]-'a';
int y=edge[i].wrd[len-]-'a';
indeg[x]++; outdeg[y]++;
mark[x]=true; mark[y]=true; edge[i].to=y; edge[i].nxt=head[x]; head[x]=i; //建边 int fx=findSet(x);
int fy=findSet(y);
if(fx!=fy)
father[fx]=fy;
}
if(judge()==){
printf("***\n");
continue;
}
DFS(src,-);
for(int i=tot-;i>;i--)
printf("%s.",res[i]);
printf("%s\n",res[]);
}
return ;
}
POJ 2337 Catenyms (欧拉回路)的更多相关文章
- POJ 2337 Catenyms
http://poj.org/problem?id=2337 题意: 判断给出的单词能否首尾相连,输出字典序最小的欧拉路径. 思路: 因为要按字典序大小输出路径,所以先将字符串排序,这样加边的时候就会 ...
- POJ 2337 Catenyms(欧拉回(通)路:路径输出+最小字典序)
题目链接:http://poj.org/problem?id=2337 题目大意:给你n个字符串,只有字符串首和尾相同才能连接起来.请你以最小字典序输出连接好的单词. 解题思路:跟POJ1386一个意 ...
- poj 2337 Catenyms 【欧拉路径】
题目链接:http://poj.org/problem?id=2337 题意:给定一些单词,假设一个单词的尾字母与还有一个的首字母同样则能够连接.问能否够每一个单词用一次,将全部单词连接,能够则输出字 ...
- POJ 2337 Catenyms(有向欧拉图:输出欧拉路径)
题目链接>>>>>> 题目大意: 给出一些字符串,问能否将这些字符串 按照 词语接龙,首尾相接 的规则 使得每个字符串出现一次 如果可以 按字典序输出这个字符串 ...
- POJ 2337 Catenyms (有向图欧拉路径,求字典序最小的解)
Catenyms Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8756 Accepted: 2306 Descript ...
- POJ 2337 Catenyms (欧拉图)
本文链接http://i.cnblogs.com/EditPosts.aspx?postid=5402042 题意: 给你N个单词,让你把这些单词排成一个序列,使得每个单词的第一个字母和上一个字单词的 ...
- POJ 2337 Catenyms(有向图的欧拉通路)
题意:给n个字符串(3<=n<=1000),当字符串str[i]的尾字符与str[j]的首字符一样时,可用dot连接.判断用所有字符串一次且仅一次,连接成一串.若可以,输出答案的最小字典序 ...
- Poj 2337 Catenyms(有向图DFS求欧拉通路)
题意: 给定n个单词, 问是否存在一条欧拉通路(如acm,matal,lack), 如果存在, 输出字典序最小的一条. 分析: 这题可以看作http://www.cnblogs.com/Jadon97 ...
- poj 2337 && zoj 1919 欧拉回路+连通性判断
题目要求按字典序排列,而且可能有重边 所以一开始就将数组从大到小排列,那么我将字符串加入链表时就会令小的不断前移,大的被挤到后面 这里有一点问题就是我一开始使用的是qsort: int cmp(con ...
随机推荐
- 条件随机场CRF HMM,MEMM的区别
http://blog.sina.com.cn/s/blog_605f5b4f010109z3.html 首先,CRF,HMM(隐马模型),MEMM(最大熵隐马模型)都常用来做序列标注的建模,像词性标 ...
- 双数组Trie树(DoubleArrayTrie)Java实现
http://www.hankcs.com/program/java/%E5%8F%8C%E6%95%B0%E7%BB%84trie%E6%A0%91doublearraytriejava%E5%AE ...
- VS2010自带的性能分析工具分析.NET程序的性能
这篇博文给大家分享的是,如何使用VS自带的性能分析工具来分析我们编写的.NET程序,一边找出程序性能的瓶颈,改善代码的质量.在实际开发中,性能真的很重要,往往决定一个产品的生死~良好的用户体验的基础之 ...
- VIM的buffers
原文:http://ju.outofmemory.cn/entry/13522 重新在不同的 tab 中打开多个关闭的buffer 文件, https://stackoverflow.com/ques ...
- Hibernate: No Session found for current thread
在Struts2+Hibernate+Srping项目中经常会遇到这种问题 我知道的一种情况是: Spring的事务配置中没有配置好异常出现处的路径 <aop:advisor pointcut= ...
- [Canvas]更多的球
欲观看动态效果请点此下载代码并用Chrome或者Firefox打开. 图例: 代码: <!DOCTYPE html> <html lang="utf-8"> ...
- Disqus评论框改造工程-Jekyll等静态博客实现Disqus代理访问
文章最初发表于szhshp的第三边境研究所转载请注明 关于博客评论 六月多说挂了,地球人都知道. 倡言.云跟帖.来必力都很烂,地球人都知道. 转Disqus的都是人才. Disqus使用中遇到的问题 ...
- js 时间加减
//js格式化时间 "yyyy-MM-dd hh:mm:ss" Date.prototype.Format = function (fmt) { var o = { "M ...
- vmware产品框架-计算中心,5.1更新等
概述:SRM,5.1新特性,vCenter Operations的介绍等 5.1改进参见:http://wenku.baidu.com/view/26530362a98271fe910ef961.ht ...
- Ubuntu/Centos 系统上安装与配置Apache
一.在线安装: Ubuntu:sudo apt-get install apache2 Centos: sudo yum install apache2 二.安装后的位置: 1.服务地址:/etc/i ...