描述

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.

输入

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.

输出

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.".

样例输入

3
2
acm
ibm
3
acm
malform
mouse
2
ok
ok

样例输出

The door cannot be opened.
Ordering is possible.
The door cannot be opened.
解题:
把这题转化成欧拉回路问题就可以了。。。
首先把每一行的第一个字母当做有向图的起始位置,结束的字母当做有向图的结束位置
 
比如:

acm malform mouse

那么就是 a->m  m->m
m->e  
=>  
a->m->e
再来看看这样算不合法的
o->k
o->k
就不合法了,o的出度为2,入度为0,k的入度为2,出度为0
a->m
i->m
也不合法,m的入度为2,出度为0

该问题可以转换为欧拉回路问题。
1,保证改图是连通的。
2,如果它是环形,那么它的每一个结点的出度=入度。
3,如果它是链状,那么只有它的起始结点(出度-入度=1)和结束结点(入度-出度=1)

 
 
#include <stdio.h>
#include <string.h>
int a[30];
int flag[30];
int abs(int a){
if(a<0)return -a;
else return a;
}
int find(int x){
int temp=a[x];
while(temp!=a[temp]){
temp=a[temp];
}
int v;
while(x!=a[x]){
v=a[x];
a[x]=temp;
x=v;
}
return temp;
}
void u(int x,int y){
x=find(x);
y=find(y);
if(x!=y){
a[x]=y;
}
}
int main(int argc, char *argv[])
{
int t;
int begin,end;
char ch[1001];
scanf("%d",&t);
while(t--){
int n;
int in[30],out[30];
scanf("%d",&n);
memset(flag,0,sizeof(flag));
memset(in,0,sizeof(in));
memset(out,0,sizeof(out));
for(int i=0; i<=25; i++)a[i]=i;
while(n--){
scanf("%s",ch);
int len=strlen(ch);
begin=ch[0]-'a';
end=ch[len-1]-'a';
flag[begin]=1;
flag[end]=1;
in[end]++;
out[begin]++;
u(begin,end);
}
int s=0;
for(int i=0; i<=25; i++){
if(flag[i]==1 && a[i]==i)s+=1;
}
if(s==1){
int f_in=0,f_out=0;
int k;
for( k=0; k<=25; k++){
if(flag[k]==1){
if(in[k]==out[k]){
continue;
}else if(abs(in[k]-out[k])==1){
if(in[k]-out[k]<0)f_out++;
if(in[k]-out[k]>0)f_in++;
if(f_out>1 || f_in>1)break;
}else if(abs(in[k]-out[k])>1){
break;
}
}
}
if(k==26){
puts("Ordering is possible.");
}else{
puts("The door cannot be opened.");
}
}else{
puts("The door cannot be opened.");
}
}
return 0;
}

最新文章

  1. 全新 Mac 安装指南(编程篇)(环境变量、Shell 终端、SSH 远程连接)
  2. HttpClient——Get,Post
  3. sudo,linux 新建账号,并开通ssh登录
  4. 在.Net中进行跨线程的控件操作(上篇:Control.Invoke)
  5. 关于《Cocos2d-x建工程时避免copy文件夹和库》的更新
  6. 设计模式六大原则——迪米特法则(LoD)
  7. Java面向对象基础二
  8. linux服务器操作系统,在相同环境下,哪个做lamp服务器更稳定点?哪个版本更稳定?
  9. Android设计模式总结
  10. 如何删除github上的某个文件夹
  11. 【SoftwareTesting】Homework3
  12. CMDB-(paramiko模块 -- 实现ssh连接)
  13. Webpack与其他打包工具的区别
  14. js 遍历EL表达式 list对象
  15. Ceph的BlueStore总体介绍
  16. A - 取(m堆)石子游戏
  17. JavaScript 之 ScriptManager.RegisterStartupScript的应用
  18. elasticsearch 服务安全配置
  19. phpstorm10激活加汉化
  20. BZOJ1228 [SDOI2009]E&amp;D

热门文章

  1. C,C++面试题2
  2. 系统蓝屏stop:ox000007B错误解决方案
  3. python抓网页数据【ref:http://www.1point3acres.com/bbs/thread-83337-1-1.html】
  4. Spring Boot专题背景简介
  5. ANE打包心得
  6. Javascript获取select的选中值和选中文本(转载)
  7. 数数(高维DP)
  8. 域适应(Domain adaptation)
  9. 安装npm及cnpm
  10. CSS动态定位