poj 2337 Catenyms

40 篇文章 0 订阅
4 篇文章 0 订阅


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

题目大意:

给一串单词,让它们按首尾字母相同的顺序排列,开始的单词首字母要尽量小


首先是一个我感觉不太完美的代码,没有用并查集去判断是否只有一个根节点,但是AC了,只能说测试数据没弄这个坑

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define N 1001
int n,m;
struct node{
    int v,next,index,vis;
};
node p[N];
int head[N],cnt1,ans[N],cnt2;
int in[N],out[N];
string str[N];
void Add(int u,int v,int index)
{
    p[cnt1].index=index;
    p[cnt1].next=head[u];
    p[cnt1].v=v;
    p[cnt1].vis=0;
    head[u]=cnt1++;
}
void euler(int u)
{
    int i;
    for (i=head[u];i!=-1;i=p[i].next)
    {
        if (p[i].vis==0)
        {
            p[i].vis=1;
            euler(p[i].v);
            ans[cnt2++]=p[i].index;
        }
    }
}
int main()
{
    int t,n,i;
    scanf("%d",&t);
    while (t--)
    {
        int tmin=N;
        scanf("%d\n",&n);
        for (i=0;i<n;i++)
            cin>>str[i];
        sort(str,str+n);
        cnt1=cnt2=0;
        memset(head,-1,sizeof(head));
        memset(in,0,sizeof(in));
        memset(out,0,sizeof(out));
        for (i=n-1;i>=0;i--)
        {
            int u=str[i][0]-'a';
            int v=str[i][str[i].length()-1]-'a';
            Add(u,v,i);
            in[v]++,out[u]++;
            tmin=min(tmin,u);       //用小值作为欧拉路径的起始点
            tmin=min(tmin,v);
        }
        int a=0,b=0;
        for (i=0;i<26;i++)
        {
            if (in[i]-out[i]==1)
                a++;
            else if (in[i]+1==out[i])
                {
                    a++;
                    tmin=i;         //确定一下起始点
                }
            else if (in[i]!=out[i])
                b++;
        }
        if (!(b==0||a==2))
        {
             printf("***\n");
             continue;
        }
        //printf("%d\n",tmin);
        euler(tmin);
        if (cnt2!=n)
        {
             printf("***\n");
             continue;
        }
        for (i=n-1;i>=0;i--)
        {
            cout<<str[ans[i]];
            if (i)
                printf(".");
            else
                printf("\n");
        }
    }
    return 0;
}

下面是完整的代码

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define N 1001
#define M 30
int n,m;
struct node{
    int v,next,index,vis;
};
node p[N];
int head[N],cnt1,ans[N],cnt2;
int in[M],out[M],rt[N],vis[M];
string str[N];
int Find(int x)
{
    return x==rt[x]?x:Find(rt[x]);
}
void Union(int x,int y)
{
    int tx=Find(x);
    int ty=Find(y);
    if (tx!=ty)
        rt[ty]=tx;
}
void Add(int u,int v,int index)
{
    p[cnt1].index=index;
    p[cnt1].next=head[u];
    p[cnt1].v=v;
    p[cnt1].vis=0;
    head[u]=cnt1++;
}
void euler(int u)
{
    int i;
    for (i=head[u];i!=-1;i=p[i].next)
    {
        if (p[i].vis==0)
        {
            p[i].vis=1;
            euler(p[i].v);
            ans[cnt2++]=p[i].index;
        }
    }
}
int main()
{
    int t,n,i;
    scanf("%d",&t);
    while (t--)
    {
        int tmin=N;
        scanf("%d\n",&n);
        for (i=0;i<n;i++)
        {
             cin>>str[i];
             rt[i]=i;
        }
        sort(str,str+n);
        cnt1=cnt2=0;
        memset(head,-1,sizeof(head));
        memset(in,0,sizeof(in));
        memset(out,0,sizeof(out));
        memset(vis,0,sizeof(vis));
        for (i=n-1;i>=0;i--)
        {
            int u=str[i][0]-'a';
            int v=str[i][str[i].length()-1]-'a';
            Add(u,v,i);
            Union(u,v);
            in[v]++,out[u]++;
            vis[v]=vis[u]=1;
            tmin=min(tmin,u);
            tmin=min(tmin,v);
        }
        int tr=0;
        for (i=0;i<26;i++)
            if (vis[i]&&rt[i]==i)
              tr++;
        if (tr>1)
        {
             printf("***\n");
             continue;
        }
        int a=0,b=0;
        for (i=0;i<26;i++)
        {
            if (in[i]-out[i]==1)
                a++;
            else if (in[i]+1==out[i])
                {
                    a++;
                    tmin=i;         //确定一下起始点
                }
            else if (in[i]!=out[i])
                b++;
        }
        if (!(b==0||a==2))
        {
             printf("***\n");
             continue;
        }
        //printf("%d\n",tmin);
        euler(tmin);
        if (cnt2!=n)
        {
             printf("***\n");
             continue;
        }
        for (i=n-1;i>=0;i--)
        {
            cout<<str[ans[i]];
            if (i)
                printf(".");
            else
                printf("\n");
        }
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值