您的位置:首页 > 编程语言 > C语言/C++

POJ 2337 Catenyms

2016-07-17 16:46 323 查看
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
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

先判断一遍欧拉路,再dfs一次。dfs以后顺便判断图是否连通,不连通的话输出“***”。

在录入边的时候要先排序,按字典序从大到小排。可以保证记录的是最小字典序解法。

但是排序的时候如果直接写,不加cmp函数的话,要把输入的字符串写成string型,不能用char型,网上查了一下好像是说char型存储字符串本身就是不正确的?(虽然有的时候答案是正确的~)(而且这样输入输出不能用cstdio~)

初始化好麻烦的,一不小心就漏掉了~

或者这道题本身就很麻烦?

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<string>
using namespace std;

string str[1010];
int in[30],out[30],cnt,ans[1010],head[30],tot;

struct Edge
{
int to,next; //该单词末尾字母(下一节点),该点的第几条边
int index; //字典序
bool b; //是否加入
}edge[2010];

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].b=0;
head[u]=tot++;
}

void dfs(int u)
{
for(int i=head[u];i!=-1;i=edge[i].next) //遍历该点所连的每一条边(相当于邻接表),从后往前
if(!edge[i].b)
{
edge[i].b=true;
dfs(edge[i].to); //遍历下一节点
ans[cnt++]=edge[i].index; //记录
}
}

int main()
{
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) //只要有差值超过1的就不成立
cc1=3;
}
if(!((cc1==0 && cc2==0) || (cc1==1 && cc2==1)))
{
printf("***\n");continue;
}
cnt=0;
dfs(start);
if(cnt!=n) //判断是否连通,没有完全遍历的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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C++ 图论 欧拉路