hdu play on words

来源:互联网 发布:mac网游排行榜 编辑:程序博客网 时间:2024/05/28 05:16

Play on Words

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.
/*
可以将单词看做有向图的一条边,首位字母看作是两个点,然后判断此图中是否存在一条欧拉路或者欧拉回路;
对于有向图存在欧拉路的条件:图是连通的,并且存在一个节点的入度比出度大一,一个节点的入度比出度小一,其余节点入度等于出度
欧拉回路的条件:图是连通的,并且所有节点的入度等于出度;
这里所说的欧拉路,欧拉回路都是单向的;
判断图是否连通的,可以直接用并查集来判断;如果根节点只有一个,就说明图是连通;
*/
#include<stdio.h>#include<string.h>int pre[30];int indegree[30];int oudegree[30];char vis[30];int find(int root){int son,tmp;son = root;while(root!=pre[root])root = pre[root];while(son!=root){tmp = pre[son];pre[son] = root;son = tmp;}return root;}int is_exist(char x){int len = strlen(vis);for(int i=0;i<len;i++){if(x == vis[i])return 1;}return 0;}int main(){int t,n;int i,k,j;int x,y,tmp,p1,p2;char buf[1010];scanf("%d",&t);while(t--){scanf("%d",&n);getchar();for(i=0;i<30;i++)pre[i] = i;memset(indegree,0,sizeof(indegree));memset(oudegree,0,sizeof(oudegree));memset(vis,0,sizeof(vis));k = 0;for(i=0;i<n;i++){scanf("%s",buf);p1 = buf[0] - 'a';p2 = buf[strlen(buf)-1] - 'a';x = find(p1);y = find(p2);if(x!=y)pre[x] = y;oudegree[p1]++;indegree[p2]++;if(!is_exist(buf[0]))vis[k++] = buf[0];if(!is_exist(buf[strlen(buf)-1]))vis[k++] = buf[strlen(buf)-1];}vis[k] = '\0';int cont = 0;i = 0;tmp = p1 = p2 = 0;while(vis[i]!='\0'){j = vis[i] - 'a';if(pre[j]==j)//判断图的连通性cont++;if(indegree[j]==oudegree[j])//找入度等于出度的节点tmp++;if(indegree[j]+1==oudegree[j])p1++;if(indegree[j]==oudegree[j]+1)p2++;i++;}if(cont==1&&tmp==k)//欧拉回路printf("Ordering is possible.\n");else if(cont==1&&p1==1&&p2==1&&tmp==k-2)//欧拉路printf("Ordering is possible.\n");elseprintf("The door cannot be opened.\n");}return 0;}
0 0
原创粉丝点击