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
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 #include<cstdio>
  2 #include<vector>
  3 #include<cstring>
  4 #include<stack>
  5 #include<cstdlib>
  6 #include<algorithm>
  7 using namespace std;
  8
  9 struct str
 10 {
 11     char word[35];
 12 };
 13
 14 struct Edge
 15 {
 16     int u,v;
 17 };
 18
 19 str s[1005];
 20 int in[27],out[27];
 21 bool vis[1005];
 22 vector<Edge>edges;
 23 vector<int>G[1005];
 24 stack<int>S;
 25 int n,beg;
 26 bool flag;
 27
 28 bool cmp(str a,str b)
 29 {
 30     return strcmp(a.word,b.word)<0;
 31 }
 32
 33 void init()
 34 {
 35     edges.clear();
 36     while(!S.empty())S.pop();
 37     for(int i=0;i<26;i++)G[i].clear();
 38     memset(in,0,sizeof(in));
 39     memset(out,0,sizeof(out));
 40     memset(vis,0,sizeof(vis));
 41     flag=0;
 42 }
 43
 44 void dfs(int u)
 45 {
 46     for(int i=0;i<G[u].size();i++)
 47     {
 48         int x=G[u][i];
 49         if(!vis[x])
 50         {
 51             vis[x]=1;
 52             dfs(edges[x].v);
 53             S.push(x);
 54         }
 55     }
 56 }
 57
 58 void solve()
 59 {
 60     int cnt1=0,cnt2=0;
 61     for(int i=0;i<26;i++)
 62     {
 63         if(in[i]==out[i])
 64             continue;
 65         else if(out[i]-in[i]==1)
 66         {
 67             cnt1++;
 68                 beg=i;
 69             if(cnt1>=2)
 70                 return;
 71         }
 72         else if(in[i]-out[i]==1)
 73         {
 74             cnt2++;
 75             if(cnt2>=2)
 76                 return;
 77         }
 78         else
 79             return;
 80     }
 81     if(cnt1>1||cnt2>1)return;
 82     if(cnt1==0)
 83     for(int i=0;i<26;i++)
 84         if(out[i])
 85         {
 86             beg=i;
 87             break;
 88         }
 89     flag=1;
 90 }
 91
 92 int main()
 93 {
 94     //freopen("in.txt","r",stdin);
 95     int T;
 96     scanf("%d",&T);
 97     while(T--)
 98     {
 99         init();
100         scanf("%d",&n);
101         for(int i=0;i<n;i++)
102             scanf("%s",s[i].word);
103         sort(s,s+n,cmp);
104         for(int i=0;i<n;i++)
105         {
106             int u=s[i].word[0]-‘a‘;
107             int v=s[i].word[strlen(s[i].word)-1]-‘a‘;
108             in[v]++;
109             out[u]++;
110             edges.push_back((Edge){u,v});
111             G[u].push_back(i);
112         }
113         solve();
114         if(!flag)
115         {
116             printf("***\n");
117             continue;
118         }
119         dfs(beg);
120         if(S.size()!=n)
121         {
122             printf("***\n");
123             continue;
124         }
125         else
126         {
127             bool key=0;
128             while(!S.empty())
129             {
130                 int x=S.top();
131                 if(key)
132                     printf(".");
133                 printf("%s",s[x].word);
134                 key=1;
135                 S.pop();
136             }
137             printf("\n");
138         }
139     }
140     return 0;
141 }
时间: 2024-10-13 09:07:00

poj 2337 Catenyms(欧拉路径)的相关文章

POJ 2337 Catenyms (欧拉回路+并查集)

题目地址:POJ 2337 这题跟POJ 1386差不多,只不过这题多一个输出路径而已. 按字母来建边,每个单词的首字母和尾字母加边.先判断是否连通,然后判断每个字母的入度和出度不能出现差的绝对值大于2,然后入度和出度差的绝对值为1的不能超过两个.就可以形成欧拉路径 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <alg

poj 2337 Catenyms 【欧拉路径】

题目链接:http://poj.org/problem?id=2337 题意:给定一些单词,假设一个单词的尾字母与还有一个的首字母同样则能够连接.问能否够每一个单词用一次,将全部单词连接,能够则输出字典序最小的序列. 代码: (bin 神的板子) #include <stdio.h> #include <ctime> #include <math.h> #include <limits.h> #include <complex> #include

POJ 2337 Catenyms (有向图欧拉通路)

Catenyms Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9914   Accepted: 2588 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 e

POJ 2337 Catenyms

http://poj.org/problem?id=2337 题意: 判断给出的单词能否首尾相连,输出字典序最小的欧拉路径. 思路: 因为要按字典序大小输出路径,所以先将字符串排序,这样加边的时候就会优先加字典序小的边,dfs的时候也就会先走字典序小的边. 判断一下图的连通性以及是否存在欧拉道路. 然后进行深搜寻找欧拉路径,因为欧拉路径时要逆序输出的,所以这里需要先保存起来,最后再次逆序输出即可得到正确的路径. 1 #include<iostream> 2 #include<algori

POJ 2337 Catenyms 欧拉通路

题目链接:点击打开链接 题意: 把输入的n个由小写字母构成的字符串连成字典序最小的一句话,使得所有字符串都恰好出现一次且相邻两个字符串相邻的字母相同 思路: 比如abcd,我们认为是这样一条边:a->d 所以我们在a->d间建一条边. 1.如:abcd, dfgh, 那么得到的边就是 a->d, d->h. 而题目的目标是每个字符串恰好用一次,即每条边恰好用一次.也就是找一条欧拉通路 2.我们只需要关心字符串的首尾2个字母,所以我们认为图里只有26个节点. 3.判断欧拉通路是否存在

poj 2337 之 有向图 欧拉路径输出

/* poj 2337 之 有向图 欧拉路径输出  每个单词看作一条有向边,顶点即为单词首尾字母,然后求欧拉路径即可. 1)为保证字典序,先对单词按字典序排序 2)深搜,输出单词序列 */ 1 #include <iostream> 2 #include <fstream> 3 #include <sstream> 4 #include <cstdlib> 5 #include <cstdio> 6 #include <cstddef>

poj 2337 欧拉回路按照最小字典序输出+注意为了按最小字典序怎么处理邻接表

http://poj.org/problem?id=2337 WA了好久,昨晚1点多睡不着写的,狂WA,当时是因为用邻接矩阵存储,比如aba,aa只能存下一个,这个之前还没遇到过,今天才注意到--邻接矩阵无法存储平行边, 关于欧拉回路判断看我另几篇日志或者看我的欧拉总结 再贴个输出欧拉回路的模板 其中,参数u是起点,注意如果是输出欧拉路径的话,u必须是出度比入度大一的那个点,如果输出欧拉回路,随便按要求找个就行 void euler(int u) { for(int i=head[u];i!=-

poj 2337 有向图输出欧拉路径

Catenyms Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10186   Accepted: 2650 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

POJ 2337 &amp;&amp; ZOJ 1919--Catenyms 【有向图 &amp;&amp; 欧拉路判断 &amp;&amp; 欧拉路径】

Catenyms Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10427   Accepted: 2726 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