-和尚-

导航

Play on Words

题目:

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

3
2
acm
ibm
3
acm
malform
mouse
2
ok
ok

Sample Output

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

 

 

 

 

 

这道题目意思就是:在所有的单词当中存在一条这样的回路(前一单词的最后的字母与下一单词的头字母相同,形成一条字母链(所有的字母都要包含))这样们就可以打开!否则就不能打开!你的任务就是判断能否打开;

这道题可以考虑欧拉回路!

欧拉回路:对于无向图每个顶点的度都是偶数度,或者只有两个点的度为奇数度;有向图:所有的点出度等于入度!或者有一个点的入度-出度=1(终点),一个点的出度-入度=1;(起点);

对于本题找欧拉回路我们可以利用并查集来找!

代码:

#include"stdio.h"

#include"string.h"

int out[27],in[27],fa[27],map[27];

void init()//初始化

{

       for(int i=0;i<26;i++)

              out[i]=0,in[i]=0,fa[i]=i,map[i]=0;

}

int find(int t)//并查集

{

       if(fa[t]!=t)

              fa[t]=find(fa[t]);

       return fa[t];

}

void unito(int x,int y)//合并

{

       int fx=find(x),fy=find(y);

       fa[fy]=fx;

}

int main()

{

       int T,i,j,k,n,x,y;char str[1005];

       scanf("%d",&T);

       while(T--)

       {

              scanf("%d",&n);

              init();

              for(i=0;i<n;i++)

              {

                     scanf("%s",str);

                     x=str[0]-'a';y=str[strlen(str)-1]-'a';

                     out[x]++;in[y]++;//建图,收尾字母建在图上

                     map[x]=map[y]=1;//当前标记

                     unito(x,y);

              }

              i=0,j=0,k=1,x=0,y=0;

              for(i=0;i<26;i++)

              {

                     if(map[i])

                     {

                            n=find(i);//看最后节点

                            if(out[i]+1==in[i])//这3个if是求出入度的!

                                   x++;

                            if(in[i]+1==out[i])

                                   y++;

                            if(in[i]!=out[i])

                                   j++;

                     }

              }

              for(i=0;i<26;i++)

              {

                     if(map[i])

                     {

                            if(fa[i]!=n)//最后节点都联通了

                                   k=0;

                     }

              }

              if(((x==1&&y==1&&j==2)||j==0)&&k==1)

                     printf("Ordering is possible.\n");

              else

                     printf("The door cannot be opened.\n");

       }

       return 0;

}

posted on 2012-08-05 17:04  -和尚-  阅读(239)  评论(0编辑  收藏  举报