Catenyms(有向欧拉图+判断回路+打印路径)

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
***

题意:

给你一组N个单词,现在要你输出这样一组单词序列。该序列包含了所有N个单词,且该序列中的前一个单词的最后一个字母与后一个单词的第一个字母相同。如果存在多个这种首尾相连的序列,就输出字典序最小的那个即可。

思路:

首先确定题目为有向图问题,我们把每个单词的首尾单词看作两个结点,总的来说就是找一条欧拉路径。首先呢需要判断有向图是弱连通的,所以用到了并查集来进行判断,其次就是我们在输入之后,对输入的单词按字典序进行排序,在结构体里面完成这个操作,那么最后如果该图是弱连通且所有点的度数都符合欧拉图的要求时,就可以输出欧拉路径了。

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=1000+10;
struct Word
{
    int len;
    char s[20+5];
    bool operator <(const Word &b)const  //对单词进行排序 
    {
        return strcmp(s,b.s)<0;
    }
}words[maxn];

struct Edge //邻接矩阵记录边 
{
    int u,v;//u表示起点,v表示终点
    bool vis;
}edges[maxn];


int fa[26+5];          //并查集
int in[26+5],out[26+5];//入度,出度
bool mark[26+5];       //mark[i]=true,表i这个字母被用到作为图顶点了
int m;//单词数,即边数
int cnt,ans[maxn];//记录输出单词的序号,逆序
int findset(int u)  //查找根节点 
{
    if(fa[u]==-1) return u;
    return fa[u]=findset(fa[u]);
}
bool ok()//判断是否弱连通图
{
    int k=0;
    for(int i=0;i<26;i++)if(mark[i])
        if(findset(i)==i) k++;
    return k==1;
}
void euler(int u)
{
    for(int i=0;i<m;i++)if(!edges[i].vis && edges[i].u==u)//本边未走过且起点吻合
    {
        edges[i].vis=true;
        euler(edges[i].v);
        ans[cnt++]=i;  //记录走的序号 
    }
}
int main()
{
    int T; scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&m);
        for(int i=0;i<m;i++)
        {
            scanf("%s",words[i].s);
            words[i].len=strlen(words[i].s);
        }
        sort(words,words+m);

        memset(fa,-1,sizeof(fa));
        memset(mark,0,sizeof(mark));
        memset(in,0,sizeof(in));
        memset(out,0,sizeof(out));
        for(int i=0;i<m;i++)
        {
            int u,v;
            u=words[i].s[0]-'a';
            v=words[i].s[words[i].len-1]-'a';
            edges[i].u=u;
            edges[i].v=v;
            edges[i].vis=false;     //表示该边还未被访问
            mark[u]=mark[v]=true;
            in[v]++, out[u]++;
            u=findset(u), v=findset(v);
            if(u!=v) fa[u]=v;       //合并连通分量
        }

        int start=edges[0].u;//欧拉路径起始点的编号
        int i,c1=0,c2=0;                //错误1,这里c1与c2忘了初始化
        for(i=0;i<26;i++)if(mark[i])
        {
            if(in[i]==out[i]) continue;
            else if(in[i]-out[i]==1) c1++;
            else if(out[i]-in[i]==1) c2++,start=i;
            else break;
        }
        if(i==26&&((c1==c2&&c1==0)||(c1==c2&&c1==1)) && ok() )//所有点度符合要求且弱连通
        {
            cnt=0;
            euler(start);
            printf("%s",words[ans[cnt-1]].s);
            for(int i=cnt-2;i>=0;i--)
                printf(".%s",words[ans[i]].s);
            printf("\n");
        }
        else printf("***\n");
    }
    return 0;
}
地址: 点击打开链接


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值