您的位置:首页 > 其它

POJ 2337 Catenyms (有向图欧拉路径,求字典序最小的解)

2014-02-03 13:51 399 查看
Catenyms

Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 8756Accepted: 2306
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
***

Source

Waterloo local 2003.01.25

把26个小写字母当成点,每个单词就是一条边。

然后就是求欧拉路径。

为了保证字典序最小,要先排序,加边要按照顺序加。

而且求解的dfs起点要选择下,选择最小的。

/* ***********************************************
Author        :kuangbin
Created Time  :2014-2-3 13:12:43
File Name     :E:\2014ACM\专题学习\图论\欧拉路\有向图\POJ2337.cpp
************************************************ */

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
struct Edge
{
int to,next;
int index;
bool flag;
}edge[2010];
int head[30],tot;
void init()
{
tot = 0;
memset(head,-1,sizeof(head));
}
void addedge(int u,int v,int index)
{
edge[tot].to = v;
edge[tot].next = head[u];
edge[tot].index = index;
edge[tot].flag = false;
head[u] = tot++;
}
string str[1010];
int in[30],out[30];
int cnt;
int ans[1010];
void dfs(int u)
{
for(int i = head[u] ;i != -1;i = edge[i].next)
if(!edge[i].flag)
{
edge[i].flag = true;
dfs(edge[i].to);
ans[cnt++] = edge[i].index;
}
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int T,n;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i = 0;i < n;i++)
cin>>str[i];
sort(str,str+n);//要输出字典序最小的解,先按照字典序排序
init();
memset(in,0,sizeof(in));
memset(out,0,sizeof(out));
int start = 100;
for(int i = n-1;i >= 0;i--)//字典序大的先加入
{
int u = str[i][0] - 'a';
int v = str[i][str[i].length() - 1] - 'a';
addedge(u,v,i);
out[u]++;
in[v]++;
if(u < start)start = u;
if(v < start)start = v;
}
int cc1 = 0, cc2 = 0;
for(int i = 0;i < 26;i++)
{
if(out[i] - in[i] == 1)
{
cc1++;
start = i;//如果有一个出度比入度大1的点,就从这个点出发,否则从最小的点出发
}
else if(out[i] - in[i] == -1)
cc2++;
else if(out[i] - in[i] != 0)
cc1 = 3;
}
if(! ( (cc1 == 0 && cc2 == 0) || (cc1 == 1 && cc2 == 1) ))
{
printf("***\n");
continue;
}
cnt = 0;
dfs(start);
if(cnt != n)//判断是否连通
{
printf("***\n");
continue;
}
for(int i = cnt-1; i >= 0;i--)
{
cout<<str[ans[i]];
if(i > 0)printf(".");
else printf("\n");
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: