uva 10129 - Play on Words

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 Specification

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 Specification

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

Output for the Sample Input

The door cannot be opened.
Ordering is possible.
The door cannot be opened.
把单词的首尾字母看成2个端点中间为线段,抽象成有有向图的边,
再根据有向图的欧拉路条件
1。连通图
2。所有节点入度等于出度(欧拉回路)或者只有1个点入度比出度小1一个点出度比入度小1,其余节点出度等于入度。
判断连通性纠结了下,有向图有点晕了,可以用dfs或者并查集,
dfs的话如果存在出度比入度大一的哪个点的话,以那个点为起点遍历,否则随便找个点为起点,标记访问过的点,如果存在未访问过的点则不联通。
#include<stdio.h>
#include<string.h>
#include<math.h>
int visit[26],a[26][26]; 
void dfs(int x)
{int i;
 visit[x]=0;
 for (i=0;i<26;i++)
 if ((visit[i]==1)&&(a[x][i]==1)) dfs(i);
}
int main()
{int f,t,k1,k2,num,n,i,j,l,in[26],out[26];
 char s[1010];
 scanf("%d",&t);
 while (t--)
 {
  for (i=0;i<26;i++)
  for (j=0;j<26;j++)
  a[i][j]=0;
  for (i=0;i<26;i++) 
  {in[i]=0; out[i]=0; visit[i]=0;}
  scanf("%d\n",&n);
  while (n--)
  {gets(s); l=strlen(s);
   k1=s[0]-'a'; k2=s[l-1]-'a';
   ++in[k1]; ++out[k2];
   visit[k1]=1; visit[k2]=1;
   a[k1][k2]=1;
   num=k1;
  }
  for (i=0;i<26;i++)
  in[i]=in[i]-out[i];
  f=0; k1=0; k2=0; 
  for (i=0;i<26;i++)
  {if (in[i]==-1) ++k1;
   else if (in[i]==1)  {++k2;num=i;} //因为是有向图,如果不是欧拉回路的情况,随便找个点为起点dfs不一定能遍历所有点,如3 a b b c c d只能以a为起点否则就会判断连通性错误
   else if (in[i]!=0)  f=1;
  }
  
  if (f==0) 
  {
   if ((k1+k2==0) || ((k1==1)&&(k2==1)))
   {dfs(num);
    for (i=0;i<26;i++)
    f+=visit[i];
   }
   else f=1;
  }
  if (f!=0) printf("The door cannot be opened.\n");
	   else printf("Ordering is possible.\n");
  
 }
 return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值