poj1116 Play on Words

來源:互聯網
上載者:User
 
New~ 歡迎熱愛編程的"高考少年"報考——"杭州電子科技大學-電腦學院"~
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 DescriptionSome 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.  

InputThe 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.  

OutputYour 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.主要就是要一個判連通,就是一個歐拉迴路,水題直接上代碼
#include <iostream>#include<stdio.h>#include<string.h>#include<math.h>using namespace std;char str[1006];int down[30];int fin(int x){   if(down[x]!=x)   {         down[x]=fin(down[x]);   }   return down[x];}int main(){      int t,in[30],out[30],visit[30];      int i,n,ge,k;      scanf("%d",&t);      while(t--)      {            for(i=0;i<30;i++)            {                  down[i]=i;                  in[i]=0;                  out[i]=0;                  visit[i]=0;            }            memset(str,'\0',sizeof(str));            scanf("%d",&n);            gets(str);ge=k=0;            memset(str,'\0',sizeof(str));            //printf("%d ",n);            for(i=0;i<n;i++)            {                  gets(str);                 // printf("%s",str);                 // printf("%d i\n",i);                 int a=fin(str[0]-'a');                 int b=fin(str[strlen(str)-1]-'a');                  in[str[0]-'a']++;                  out[str[strlen(str)-1]-'a' ]++;                  visit[str[0]-'a']=1;                  visit[str[strlen(str)-1]-'a']=1;                  if(a!=b)                  {                        down[a]=b;                        k++;                  }                  //printf("  %d %d \n",str[0]-'a',strlen(str)-1);                  memset(str, '\0',sizeof(str));            }            for(i=0;i<26;i++)            {                  if(visit[i])                  {                        ge++;                  }            }            bool zhe=true;            //printf("%d %d \n",k,ge);            k++;            if(k==ge)                  zhe=true;            else            {                 printf("The door cannot be opened.\n");                 continue;            }            int s=-1,e=-1;           // for(i=0;i<26;i++)                 // printf("%d %d %d i\n",in[i],out[i],i);            for(i=0;i<26;i++)            {                  if(in[i]==out[i])                  {                              continue;                  }                  if(fabs(in[i]-out[i])>1)                  {                        zhe=false;                        break;                  }                  else if((in[i]-out[i])==1)                  {                        if(s==-1)                        {                             s=i;                        }                        else                        {                              zhe=false;                              break;                        }                  }                  else if((out[i]-in[i])==1)                  {                        if(e==-1)                        {                              e=i;                        }                        else                        {                              zhe=false;                              break;                        }                  }            }            if(zhe)            {                  printf("Ordering is possible.\n");            }            else            {                  printf("The door cannot be opened.\n");            }      }    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.