【Jason's_ACM_解题报告】Play on Words
2015-02-18 08:21
381 查看
Play on Words
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 N that indicates the number of plates (1 <= N <= 100000). Then exactly N lines 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
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.
欧拉路径充要条件以及图的连通性练习。
此处无需输出欧拉路径,大大降低了题目难度,所以仅对各点入度出度的特殊情况进行特判,并相应的判断图的连通性即可。
我的方法是DFS判断连通性。
Liu的方法是使用并查集,往后复习到会贴上他的代码。
附代码如下:
#include<cstdio> #include<cstring> using namespace std; #define MAXL (1000+5) #define MAXN (26+5) #define CLR(x,y) (memset(x,y,sizeof(x))) int n,start; bool G[MAXN][MAXN]; bool flag[MAXN]; int d[MAXN];//'a'~'b'的度,正为出度,负为入度。 int input(){ CLR(G,false);CLR(d,0);CLR(flag,false); scanf("%d",&n); for(int i=0;i<n;i++){ char str[MAXL]; scanf("%s",str); int len=strlen(str); int x=str[0]-'a',y=str[len-1]-'a'; start=x; G[x][y]=G[y][x]=true; d[x]++;d[y]--; flag[x]=flag[y]=true; } } void dfs(int x){ flag[x]=false; for(int i=0;i<26;i++){ if(flag[i]&&G[x][i])dfs(i); } } bool check(){ int st=0,ed=0,mid=0; for(int i=0;i<26;i++){ if(d[i]==1){st++;start=i;} if(d[i]==-1)ed++; if(d[i]==0)mid++; } if(mid!=26){ if((st+mid+ed)!=26)return false; if(!(st==1&&ed==1))return false; } dfs(start); for(int i=0;i<26;i++){ if(flag[i])return false; } return true; } int main(){ int T; scanf("%d",&T); while(T--){ input(); if(check()){ printf("Ordering is possible.\n"); }else{ printf("The door cannot be opened.\n"); } } return 0; }
相关文章推荐
- 【Jason's_ACM_解题报告】Trees on the level
- Play on words 之并查集+欧拉回路解题报告
- UVa 10129 Play on Words 欧拉回路 解题报告
- 【Jason's_ACM_解题报告】Graveyard
- 【Jason's_ACM_解题报告】Network
- 【Jason's_ACM_解题报告】Division
- 【Jason's_ACM_解题报告】Spreading the Wealth
- 【Jason's_ACM_解题报告】Ananagrams
- 【Jason's_ACM_解题报告】Tree
- 【Jason's_ACM_解题报告】Maximum Product
- 【Jason's_ACM_解题报告】Defense Lines
- 【Jason's_ACM_解题报告】Commando War
- 【Jason's_ACM_解题报告】City Game
- 【Jason's_ACM_解题报告】Not so Mobile
- 【Jason's_ACM_解题报告】Fractions Again?!
- 【Jason's_ACM_解题报告】Color Length
- 【Jason's_ACM_解题报告】The Falling Leaves
- 【Jason's_ACM_解题报告】Oil Deposits
- 【Jason's_ACM_解题报告】Prime Ring Problem
- 【Jason's_ACM_解题报告】Quadtrees