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 ...
随机推荐
- 微信小程序 this.data与this.setData
一.摘要 小程序中我们会经常使用到this.data与this.setData.其中this.data是用来获取页面data对象的,而this.setData是用来更新界面的.那么他们之间的区别与联系 ...
- 如何用Javascript检测到所有的IE版本
如何用Javascript检测到所有的IE版本 function GetIEVersion() { var sAgent = window.navigator.userAgent; var Idx = ...
- Idea不能新建package的解决
右键–>new –> Mark Directory As –> Sources Root (idea需要修改一下目录的性质,改为源文件 )
- (纪录片)数学的故事 The Story of Maths (2008)
简介: 导演: Robin Dashwood编剧: Marcus du Sautoy主演: Marcus du Sautoy类型: 纪录片官方网站: www.bbc.co.uk/programmes/ ...
- (纪录片)《星际穿越》中的科学 The Science of Interstellar
简介: 导演: Gail Willumsen编剧: Gail Willumsen主演: 克里斯托弗·诺兰 / 乔纳森·诺兰 / 基普·索恩 / 马修·麦康纳类型: 纪录片 / 短片制片国家/地区: 美 ...
- ES6...扩展运算符(数组或类数组对象)
数组和类数组对象定义 数组:[] 类数组对象:只包含使用从零开始,且自然递增的整数做键名,并且定义了length表示元素个数的对象,我们就认为他是类数组对象. 数组使用 let foo_arr = [ ...
- vmware备份
http://wenku.baidu.com/view/fc317dcc050876323112128d.html vmware vcb文档 http://www.docin.com/p-423555 ...
- Node,Sockets,Cores,Threads
http://fishcried.com/2015-01-09/cpu_topology/ http://kodango.com/cpu-topology http://www.udpwork.com ...
- openerp学习笔记 视图继承(tree、form、search)
支持的视图类型:form.tree.search ... 支持的定位方法: <notebook position="inside"> ...
- java LimitedThreadPool
此线程池一直增长,直到上限,增长后不收缩(因为池子里面的线程是永生的).这个keepAliveTime参数设置的为Long.MAX_VALUE,所以池子里面的线程几乎不会因为idle而被termina ...