Play on Words

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 Specification

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 Specification

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

Output for the Sample Input

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

 

题意:输入n个单词,是否可以排成一个序列,使得每个单词的第一个字母和上一个单词的最后一个字母相同

思路:有向图的欧拉路径。

//1,判断连通(并查集)
//2,判断入度 和出度 
#include <iostream>   
#include <cstring>  
#include <cstdio>  
#include <algorithm>  
using namespace std; 
char ph[27];//并查集
int find(int x) {return ph[x]!=x?ph[x]=find(ph[x]):x;}

int main()
{
 int t,n,i,j;
 int oin[256],sd[256];
 char str[10005];
 
 scanf("%d",&t);
  while(t--)
  {
   scanf("%d",&n);     
     for(j='a';j<='z';j++) ph[j]=j;//并查集(初始化)
      memset(oin,0,sizeof(oin));
      memset(sd,0,sizeof(sd));
      int cx=26; //连通块数  
 
     for(i=0;i<n;i++)
     {
        scanf("%s",str);
        char c1=str[0],c2=str[strlen(str)-1];
        oin[c1]++; //入度 
        oin[c2]--;//出度 
 
         sd[c1]=sd[c2]=1;//标志存在 
         char s1=find(c1),s2=find(c2);//并查集判断连通
         if(s1!=s2) {ph[s1]=s2;cx--;} 
     }         
     
     int k=0,d[256];   
     for(i='a';i<='z';i++)
     {
       if(!sd[i]) cx--;//排除没有出现的字符 
       else if(oin[i]) {d[k]=oin[i];k++;}//存头部的入度和尾的出度(可能是2,可能为0)                    
     }
     if(cx==1&&(k==0||(k==2&&(d[0]==1||d[0]==-1)))) printf("Ordering is possible.\n");
     else printf("The door cannot be opened.\n"); 
  }                          
return 0;
}


发布了90 篇原创文章 · 获赞 2 · 访问量 5万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章