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

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

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.

题解: 输入一些英文单词,根据该单词的首尾字母,判断所有单词能不能连成一串,类似于成语接龙的意思。同样如果有多个重复的单词时,也必须满足这样的条件才能通过,否则都是不可能的情况。输入包括若干个案例,每个案例中最多有100000个单词。

一、无向图
每个顶点的度数都是偶数,则存在欧拉回路。

二、有向图(所有边都是单向的)
每个节顶点的入度都等于出度,则存在欧拉回路。

 三.混合图欧拉回路
  混合图欧拉回路用的是网络流。
  把该图的无向边随便定向,计算每个点的入度和出度。如果有某个点出入度之差为奇数,那么肯定不存在欧拉回路。因为欧拉回路要求每点入度 = 出度,也就是总度数为偶数,存在奇数度点必不能有欧拉回路。

什么时候可以不重复的走完所有边?

(1)对于无向图来说,就是所有点的度为偶数,或者有且只有两个点的度为奇数其他点为均为偶数

(2)对于有向图来说就是所有点的入度与出度都相等或者存在一个点入度比出度大一,存在另一个点出度比入度大一,其他点的入度与出度相等。

代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int f[26],in[26],out[26],book[26],p[26];
int getf(int v)
{
	if(f[v]!=v)
	  f[v]=getf(f[v]);
	return f[v];
}
void mer(int u,int v)
{
	int t1=getf(u);
	int t2=getf(v);
	if(t1!=t2)
	  f[t2]=t1;
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		char s[1010];
		int i,k=0,a,b,n,count=0;
		for(i=0;i<26;i++)
		   f[i]=i;//对26个字母进行初始化 
		memset(in,0,sizeof(in));
		memset(out,0,sizeof(out));
		memset(book,0,sizeof(book));
		scanf("%d",&n);
		for(i=1;i<=n;i++)
		{
			scanf("%s",s);
			a=s[0]-'a';//0到26分别表示 26个字母,把首尾字母化为数字 
			b=s[strlen(s)-1]-'a';
			mer(a,b);
			in[a]++;//把出度入度分别加1; 
			out[b]++;
			book[a]=book[b]=1;//标记已存在的字母 
		}
		for(i=0;i<26;i++)
		{
			f[i]=getf(i);
			if(book[i]&&f[i]==i)
			   count++;//计算连通分支的个数 
			if(book[i]&&in[i]!=out[i])
			    p[k++]=i;
		}
		if(count>1)//若是非连通图直接输出 
		{
			printf("The door cannot be opened.\n");
			continue;
		}
		if(k==0)//出度和入度都相等 
		{
			printf("Ordering is possible.\n");
			continue;
		}
		if(k==2&&(abs(in[p[0]]-out[p[0]])==1)&&(abs(in[p[1]]-out[p[1]])==1))//入度和初度不相等的点为2且相差为1 
		  printf("Ordering is possible.\n");
		else
		  printf("The door cannot be opened.\n");
		 
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值