HDU1116_Play on Words_并查集

来源:互联网 发布:数据透视表切片器作用 编辑:程序博客网 时间:2024/06/06 05:00

Play on Words

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8240    Accepted Submission(s): 2788


Problem 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
32acmibm3acmmalformmouse2okok
 

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

大致题意:

给出n个字符串,当一个字符串的首字母恰是另一个字符串的尾字母时,“一个字母”可以连在“另一个字母”的后面。请你判断给出的n个字符串能否连成一串。


大体思路:

这个题可以分解为两个问题解决:

1)连通性:确保所有的字符串最终串成一串

2)出入度:在一个字符串中,我们只关注它的首字母和尾字母,中间字母与实际上与解题没有关系。为了保证所有字符串被连成一串而没有岔路,需要计算每个字母的出度和入度。最多只有整个串的首尾两个字母,其出度!=入度。其他所有点的出度必然等于入度。如果恰好首字母与尾字母相同,那么所有字母的出度就都等于入度了。


#include<cstdio>#include<cstring>int s [26];int p [26];bool vis [26];int g;int cas,n;char str [1020];int unfi (int x){int r=x;while(r!=p[r]) r=p[r];int i=x,j;while(i!=r) j=p[i],p[i]=r,i=j;return r;}int main (){//freopen("in.txt","r",stdin);scanf("%d",&cas);while(cas--){g=0;for(int i=0;i<26;i++)s[i]=vis[i]=0,p[i]=i;scanf("%d",&n);while(n--){scanf(" %s",str);int len=strlen(str);int fir=str[0]-'a',las=str[len-1]-'a';s[fir]++,s[las]--;if(!vis[fir]) vis[fir]=1,g++;if(!vis[las]) vis[las]=1,g++;int fx=unfi(fir),fy=unfi(las);if(fx!=fy) g--,p[fx]=fy;}if(g!=1){printf("The door cannot be opened.\n");}else{int f=0,l=0,i=0;for(;i<26;i++){if(s[i]==0) continue;else if(s[i]==1) f++;else if(s[i]==-1) l++;else break;}if(i==26&&f==1&&l==1) printf("Ordering is possible.\n");else if(i==26&&f==0&&l==0) printf("Ordering is possible.\n");else printf("The door cannot be opened.\n");}}return 0;}



0 0
原创粉丝点击