zoj2016--Play on Words

来源:互联网 发布:windows vista premium 编辑:程序博客网 时间:2024/05/16 08:03

Description

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

There is a large number of magnetic plates on every door. Everyplate has one word written on it. The plates must be arranged intoa sequence in such a way that every word begins with the sameletter as the previous word ends. For example, the word "acm" canbe followed by the word "motorola". Your task is to write acomputer program that will read the list of words and determinewhether it is possible to arrange all of the plates in a sequence(according to the given rule) and consequently to open thedoor.
Input

The input consists of T test cases. The numberof them (T) is given on the first line of the input. Each test casebegins with a line containing a single integer number Nthatindicates the number of plates (1 <= N<= 100000). Then exactly Nlines follow, eachcontaining a single word. Each word contains at least two and atmost 1000 lowercase characters, that means only letters 'a' through'z' will appear in the word. The same word may appear several timesin the list.

Output

Your program has to determine whether it ispossible to arrange all the plates in a sequence such that thefirst letter of each word is equal to the last letter of theprevious word. All the plates from the list must be used, eachexactly once. The words mentioned several times must be used thatnumber of times.

If there exists such an ordering of plates, your program shouldprint the sentence "Ordering is possible.". Otherwise, output thesentence "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.
////////////////////////////////////////////////////////////////
# include<stdio.h>
# include<string.h>
int set[27],head[27],tail[27],mark[27];
int find(int x)
{
      if(set[x]!=x)
            set[x]=find(set[x]);
      return set[x];
}
void Combain(int x,int y)
{
      int fx,fy;
      fx=find(x);
      fy=find(y);
      if(fx==fy)
            return ;
      else if(fx<fy)
            set[fy]=fx;
      else
            set[fx]=fy;
}
int main()
{
      int i,t,n,len,flag;
      int a[27];
      char s[1003];
      scanf("%d",&t);
      while(t--)
      {
            scanf("%d",&n);
            for(i=0;i<26;i++)
            {
                  set[i]=i;
                  head[i]=0;
                  tail[i]=0;
                  mark[i]=0;
            }
            while(n--)
            {
                  scanf("%s",s);
                  len=strlen(s)-1;
                  head[s[0]-'a']++;
                  tail[s[len]-'a']++;
                  mark[s[0]-'a']=1;
                  mark[s[len]-'a']=1;
                  Combain(s[0]-'a',s[len]-'a');
            }
            flag=0;
            for(i=0;i<26;i++)
            {
                  if(mark[i]&&set[i]==i)
                      flag++;
                  if(flag>=2)
                        break;
            }
            if(flag>=2)
                  printf("The door cannot be opened.\n");
            else
            {
                  flag=0;
                  for(i=0;i<26;i++)
                  {
                        if(mark[i]&&head[i]!=tail[i])
                               a[flag++]=i;
                  }
                        if(flag==0)
                               printf("Ordering is possible.\n");
                        else if(flag>2)
                               printf("The door cannot be opened.\n");
                        else if(flag==2)
                        {
                              if(head[a[0]]-tail[a[0]]==1&&tail[a[1]]-head[a[1]]==1||tail[a[0]]-head[a[0]]==1&&head[a[1]]-tail[a[1]]==1)
                                     printf("Ordering is possible.\n");
                               else 
                                     printf("The door cannot be opened.\n");
                        }
            }
      }
      return 0;
}
 
 

 

0 0
原创粉丝点击