POJ 1386 Play on Words
Posted 一蓑烟雨任生平
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ 1386 Play on Words相关的知识,希望对你有一定的参考价值。
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 13281 | Accepted: 4461 |
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.
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.".
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
Sample Output
The door cannot be opened.
Ordering is possible.
The door cannot be opened.
Source
题意:给出n个单词,如果一个单词的尾和另一个单词的头字符相等,那么可以相连,问这n个单词是否可以排成一列。
思路:有向图判断是否存在欧拉通路
吐槽:zz的我竟然应为把这句话忘了:而调了一个小时。。。
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; char s[10000]; int fa[30]; int t,n,tot,sum,sum1,sum2; int vis[30],num[30],into[30],out[30]; int find(int x){ if(fa[x]==x) return fa[x]; return fa[x]=find(fa[x]); } int main(){ scanf("%d",&t); while(t--){ scanf("%d",&n); tot=0;sum=0;sum1=0;sum2=0; memset(vis,0,sizeof(vis)); memset(num,0,sizeof(num)); memset(out,0,sizeof(out)); memset(into,0,sizeof(into)); for(int i=1;i<=26;i++) fa[i]=i; for(int i=1;i<=n;i++){ scanf("%s",s); int x=s[0]-\'a\'+1; int y=s[strlen(s)-1]-\'a\'+1; out[x]++; into[y]++; if(!vis[x]) vis[x]=1,num[++tot]=x; if(!vis[y]) vis[y]=1,num[++tot]=y; int dx=find(x); int dy=find(y); if(dx!=dy) fa[dy]=dx; } memset(vis,0,sizeof(vis)); for(int i=1;i<=tot;i++){ int now=find(num[i]); if(!vis[now]){ vis[now]=1;sum++; } } if(sum>1){ printf("The door cannot be opened.\\n");continue; } sum=0; for(int i=1;i<=tot;i++) if(into[num[i]]!=out[num[i]]) sum++; if(sum==0){ printf("Ordering is possible.\\n");continue; } for(int i=1;i<=tot;i++) if(into[num[i]]!=out[num[i]]){ if(into[num[i]]==out[num[i]]-1) sum1=1; else if(into[num[i]]-1==out[num[i]]) sum2=1; } if(sum==2&&sum1==1&&sum2==1){ printf("Ordering is possible.\\n");continue; } else printf("The door cannot be opened.\\n"); } return 0; }
以上是关于POJ 1386 Play on Words的主要内容,如果未能解决你的问题,请参考以下文章