hdu 1116 Play on Words

来源:互联网 发布:f1ash动画制作软件 编辑:程序博客网 时间:2024/05/16 05:15

Play on Words

Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3818 Accepted Submission(s): 1229


Problem Description
Some of the secret doors contain a very interesting word puzzle. The team of archaeologists has to solve it to open that doors. Because there is no other way to open the doors, the puzzle is very important for us.

There is a large number of magnetic plates on every door. Every plate has one word written on it. The plates must be arranged into a sequence in such a way that every word begins with the same letter as the previous word ends. For example, the word ``acm'' can be followed by the word ``motorola''. Your task is to write a computer program that will read the list of words and determine whether it is possible to arrange all of the plates in a sequence (according to the given rule) and consequently to open the door.

Input
The input consists of T test cases. The number of them (T) is given on the first line of the input file. Each test case begins with a line containing a single integer number Nthat indicates the number of plates (1 <= N <= 100000). Then exactly Nlines follow, each containing a single word. Each word contains at least two and at most 1000 lowercase characters, that means only letters 'a' through 'z' will appear in the word. The same word may appear several times in the list.

Output
Your program has to determine whether it is possible to arrange all the plates in a sequence such that the first letter of each word is equal to the last letter of the previous word. All the plates from the list must be used, each exactly once. The words mentioned several times must be used that number of times.
If there exists such an ordering of plates, your program should print the sentence "Ordering is possible.". Otherwise, output the sentence "The door cannot be opened.".

Sample Input
32acmibm3acmmalformmouse2okok

Sample Output
The door cannot be opened.Ordering is possible.The door cannot be opened.

Source
Central Europe 1999

Recommend
Eddy
单词接龙。
#include <stdio.h>#include<string.h>int sets[26],in[26],out[26],sz[26],ap[26],cnt;//sets建立并查集。in记录入度。out记录出度。ap记录字母是否出现.cnt计数char words[1050];//读单词void inite()//初始化{    int i;    memset(in,0,sizeof in);    memset(out,0,sizeof out);    memset(ap,0,sizeof ap);    cnt=0;    for(i=0;i<26;i++)        sets[i]=i,sz[i]=1;}int findr(int p)//查找根节点路径压缩{    int i,j,t;    i=p;    while(i!=sets[i])        i=sets[i];        j=p;    while(j!=i)    {        t=sets[j];        sets[j]=i;        j=t;    }    return i;}void toone(int a,int b)//合并集合{    int p,q;    p=findr(a);    q=findr(b);    if(p==q)        return;    if(sz[p]>sz[q])    {        sets[q]=p;        sz[p]+=sz[q];    }    else    {        sets[p]=q;        sz[q]+=sz[p];    }    cnt--;//合并后集合数减1}int main(){    int t,i,l,n,mio,moi,flag;    int s,e;//记录首尾字母。只有首尾字母有效    scanf("%d",&t);    while(t--)    {        scanf("%d",&n);        getchar();        inite();        mio=moi=0;        flag=1;//初始为真        for(i=0;i<n;i++)        {            scanf("%s",words);            s=words[0]-'a';            if(!ap[s])//如果字母未出现过集合数加1            {                cnt++;                ap[s]=1;            }            in[s]++;            l=strlen(words);            e=words[l-1]-'a';            if(!ap[e])//同上            {                cnt++;                ap[e]=1;            }            out[e]++;            toone(s,e);            if(cnt!=1)//如果基图不连通                flag=0;        }        if(flag)        for(i=0;i<26;i++)        {            if(ap[i])//只找出现过的字母            {                if(in[i]>out[i])//记录出度数与入度数的差值                    mio=in[i]-out[i];                else if(out[i]>in[i])                    moi=out[i]-in[i];                if(mio>1||moi>1)//差值大于一直接pass                {                    flag=0;                    break;                }            }        }        if(flag)        {            if(moi==mio)//如果都为0或都为1满足条件分别为欧拉回路和欧拉路径                printf("Ordering is possible.\n");        }        else            printf("The door cannot be opened.\n");    }    return 0;}


原创粉丝点击