描述

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. MySQL如何查看连接数和状态

    查看连接数 命令:show processlist 如果要是root账号,能够看见当前所有用户的连接.如果是普通账号,只能看到自己占用的连接数.   show processlist只能是列出前100 ...

  2. Java中的多态方法

    public class Main { public void test(Object o) { System.out.println("Object"); } public vo ...

  3. docker容器中安装vi

    容器中输入vi提示 root@e36f8029c9f2:/# vi bash: vi: command not found 解决办法: 1.通过命令获取最新的软件包 apt-get update ap ...

  4. 利用Senparc.Weixin SDK 实现微信用户的授权,并获取信息

    前一段时间在学校做过一个项目,就是利用的Senparc.Weixin SDK 做的,于是翻看以前代码,虽然有注释,但是还是看的迷迷糊糊的,干脆就单步执行一遍看看是怎么实现的,然后就重新写了个简易的授权 ...

  5. GRPC .netcore

    GRPC是Google发布的一个开源.高性能.通用RPC(Remote Procedure Call)框架.提供跨语言.跨平台支持.以下以一个.NET Core Console项目演示如何使用GRPC ...

  6. [转载] C++异常处理机制

    原地址:http://blog.csdn.net/daheiantian/article/details/6530318 一.什么是异常处理 一句话:异常处理就是处理程序中的错误. 二.为什么需要异常 ...

  7. fillna()

    将下面注释掉 fillna() 函数:有一个inplace参数,默认为false,不会对原来dataframe中进行替换,为True时候会修改原来的.

  8. KVM虚拟机的日常管理与配置

    1. 查看KVM虚拟机配置文件及运行状态(1) KVM虚拟机默认配置文件位置: /etc/libvirt/qemu/ autostart目录是配置kvm虚拟机开机自启动目录. (2) virsh命令帮 ...

  9. docker部署生产环境下的tomcat

    1. dockerfile文件 FROM tomcat:7-jre8 WORKDIR /etc COPY ./Shanghai /etc/localtime WORKDIR /usr/share/zo ...

  10. (转)windows下VMware-workstation中安装CentOS

    windows下VMware-workstation中安装CentOS   windows下VMware-workstation中安装CentOS,可以分两部分,安装虚拟机和安装CentOS虚拟机.具 ...