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萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章