描述

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;
}

TOJ 1836 Play on Words的更多相关文章

  1. TOJ 2776 CD Making

    TOJ 2776题目链接http://acm.tju.edu.cn/toj/showp2776.html 这题其实就是考虑的周全性...  贡献了好几次WA, 后来想了半天才知道哪里有遗漏.最大的问题 ...

  2. poj 1836 Alignment(dp)

    题目:http://poj.org/problem?id=1836 题意:最长上升子序列问题, 站队,求踢出最少的人数后,使得队列里的人都能看到 左边的无穷远处 或者 右边的无穷远处. 代码O(n^2 ...

  3. POJ 1836 Alignment 水DP

    题目: http://poj.org/problem?id=1836 没读懂题,以为身高不能有相同的,没想到排中间的两个身高是可以相同的.. #include <stdio.h> #inc ...

  4. TOJ 1702.A Knight's Journey

    2015-06-05 问题简述: 有一个 p*q 的棋盘,一个骑士(就是中国象棋里的马)想要走完所有的格子,棋盘横向是 A...Z(其中A开始 p 个),纵向是 1...q. 原题链接:http:// ...

  5. TOJ 1139.Compromise

    2015-06-03 问题简述: 大概就是输入两段文本(用小写英文字母表示),分别用#表示一段话的结束输入,输出这两个文本的最长公共子序列. 简单的LCS问题,但是输入的是一段话了,而且公共部分比较是 ...

  6. poj 1836 Alignment(线性dp)

    题目链接:http://poj.org/problem?id=1836 思路分析:假设数组为A[0, 1, …, n],求在数组中最少去掉几个数字,构成的新数组B[0, 1, …, m]满足条件B[0 ...

  7. 优先队列运用 TOJ 4123 Job Scheduling

    链接:http://acm.tju.edu.cn/toj/showp4123.html 4123.   Job Scheduling Time Limit: 1.0 Seconds   Memory ...

  8. 最小生成树 TOJ 4117 Happy tree friends

    链接http://acm.tju.edu.cn/toj/showp4117.html 4117.   Happy tree friends Time Limit: 1.0 Seconds   Memo ...

  9. TOJ 4120 Zombies VS Plants

    链接:http://acm.tju.edu.cn/toj/showp4120.html 4120.   Zombies VS Plants Time Limit: 1.0 Seconds   Memo ...

随机推荐

  1. 洛谷 4051 [JSOI2007]字符加密(后缀数组)

    题目描述 喜欢钻研问题的JS 同学,最近又迷上了对加密方法的思考.一天,他突然想出了一种他认为是终极的加密办法:把需要加密的信息排成一圈,显然,它们有很多种不同的读法. 例如‘JSOI07’,可以读作 ...

  2. ZOJ3705:Applications

    Recently, the ACM/ICPC team of Marjar University decided to choose some new members from freshmen to ...

  3. jsp乱码的问题

    大家在JSP的开发过程中,经常出现中文乱码的问题,可能一至困扰着大家,现把JSP开发中遇到的中文乱码的问题及解决办法写出来供大家参考.首先了解一下Java中文问题的由来: Java的内核和class文 ...

  4. maven中pom.xml中的scope讲解

    一.compile:编译范围compile是默认的范围:如果没有提供一个范围,编译范围依赖在所有的classpath 中可用,同时它们也会被打包.而且这些dependency会传递到依赖的项目中. 二 ...

  5. [LeetCode 题解]: Roman to Interger

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 Given a ro ...

  6. mvc4开篇之BundleConfig(1)

    新建一个mvc4默认项目,会看到以下目录 打开App_start 里面BundleConfig.cs文件 你会看到这么一段话: 有关 Bundling 的详细信息,请访问 http://go.micr ...

  7. python的reflect反射方法

    核心内容专自:http://www.liujiangblog.com/course/python/48 在自动化测试的时候,需要从excel中读取关键字,此关键字对应一个方法,如何使用该关键字去调用真 ...

  8. Linux中Consul集群部署

    分配三台虚拟机: 192.168.5.125 192.168.5.128 192.168.5.129 在每台虚拟机上创建  /usr/consul 文件件  命令: mkdir /usr/consul ...

  9. 使用st link v2向stm32下载和调试程序

    st官网 正版ST-link/V2引脚定义和注意事项 分为ST-LINK/V2和ST-LINK/V2-ISOL两种型号 是STM8和STM32微控制器(MCU)系列的在线调试器和编程器(还是下载器.仿 ...

  10. scrapy-redis3

    原文链接:scrapy-redis使用以及剖析