Play on Words
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 10685   Accepted: 3625

Description

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.

Source

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int edge[][];
struct node{
int in,out;
}que[];
int vis[];
char str[];
int cnt_dfs; void dfs(int u){
vis[u]=;
for(int i=;i<;i++)
if(!vis[i]&&edge[u][i])
dfs(i); cnt_dfs++;
} int main(){
int T;
scanf("%d",&T); while(T--){
int n;
scanf("%d",&n);
memset(edge,,sizeof(edge));
memset(vis,,sizeof(vis));
memset(que,,sizeof(que));
int u,v;
// getchar(); for(int i=;i<=n;i++){ scanf("%s",str);
// getchar();
int len=strlen(str);
u=str[]-'a';
v=str[len-]-'a';
que[u].out++;
que[v].in++;
edge[u][v]=edge[v][u]=; } int cnt=,temp=;
// bool flag=false;
for(int i=;i<;i++){
if(que[i].in||que[i].out){
if(!temp){ temp=i;
}
cnt++;
}
}
// printf("--->%d\n",temp);
cnt_dfs=;
dfs(temp);
// printf("-->%d %d\n",cnt,cnt_dfs); if(cnt_dfs!=cnt){
printf("The door cannot be opened.\n");
continue;
}
// bool flag=false;
int cnt_s=,cnt_e=,cnt_m=;
cnt=;
for(int i=;i<;i++){
if(que[i].in==&&que[i].out==)
continue;
cnt++;
if(que[i].in==que[i].out)
cnt_m++;
else if(que[i].in-que[i].out==-)
cnt_s++;
else if(que[i].in-que[i].out==)
cnt_e++;
} if(cnt_m==cnt||(cnt_m==cnt-&&cnt_s==&&cnt_e==))
printf("Ordering is possible.\n");
else
printf("The door cannot be opened.\n"); }
return ;
}

poj1386 字符串类型的一笔画问题 欧拉回路的更多相关文章

  1. StackExchange.Redis帮助类解决方案RedisRepository封装(字符串类型数据操作)

    本文版权归博客园和作者本人共同所有,转载和爬虫请注明原文链接 http://www.cnblogs.com/tdws/tag/NoSql/ 目录 一.基础配置封装 二.String字符串类型数据操作封 ...

  2. Redis命令拾遗一(字符串类型)

    文章归博客园和作者“蜗牛”共同所有 .转载和爬虫请注明原文Redis系列链接 http://www.cnblogs.com/tdws/tag/NoSql/ Redis有五种基本数据类型.他们分别是字符 ...

  3. Java中,关于字符串类型、随机验证码、 时间类型

    一.字符串类型:String类型 定义一个字符串 String a="Hello World"; String b= new String ("Hello World&q ...

  4. 学习笔记:MySQL字符串类型

    字符串类型 a)         char和varchar 1.都需要指定字符的长度,char中的长度是字符的长度,而varchar的长度是字节的长度 2. char中指定的长度就是实际占用的长度,而 ...

  5. Redis常用命令入门1:字符串类型命令

    Redis总共有五种数据类型,在学习的时候,一定要开一个redis-cli程序,边看边练,提高效率. 一.最简单的命令 1.获得符合规则的键名列表 keys * 这里的*号,是指列出所有的键,同时*号 ...

  6. Spark1.3使用外部数据源时条件过滤只要是字符串类型的值均报错

    CREATE TEMPORARY TABLE spark_tbls USING org.apache.spark.sql.jdbc OPTIONS ( url 'jdbc:mysql://hadoop ...

  7. Redis从基础命令到实战之字符串类型

    字符串类型是Redis中最基本的数据类型,能存储任何形式的字符串和和二进制数据.本文以代码形式列举常用的操作命令,并在实践部分演示一个简单的商品管理功能,实现了通常使用关系型数据库开发的增改查功能,注 ...

  8. 后台返回字符串类型function的处理 (递归算法)

    $(function(){ $.ajax({ type: "post", url: "${ctx}/modules/fos/reference/echart", ...

  9. mysql中的字符串类型数据索引优化

    摘自 "高性能mysql" 对于一些字符串类型较长的字段搜索时, 可以参考如下方法

随机推荐

  1. warning: remote HEAD refers to nonexistent ref, unable to checkout.解决

    git branch -r origin/branch origin/hexo git checkout -b hexo origin/hexo

  2. [转载]AngularJS入门教程00:引导程序

    我们现在开始准备编写AngularJS应用——phonecat.这一步骤(步骤0),您将会熟悉重要的源代码文件,学习启动包含AngularJS种子项目的开发环境,并在浏览器端运行应用. 进入angul ...

  3. POJ 3181 Dollar Dayz(递推,两个long long)

    题意:John有N美元,有价格为1~K的工具,可以买的个数不限,问1~K组合出N的方案数. f[i = 第i中工具][j = 花费为j] = 方案数. f[i][j] = sigma{ f[i-1][ ...

  4. 动态规划专题(二)——树形DP

    前言 \(DP\)这东西真的是博大精深啊...... 简介 树形\(DP\),顾名思义,就是在树上操作的\(DP\),一般可以用\(f_i\)表示以编号为\(i\)的节点为根的子树中的最优解. 转移的 ...

  5. WARNING you have Transparen Huge Pages..

    redis启动警告: WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will c ...

  6. python序列化(数据本地存放持久性存储)和反序列化

    http://blog.csdn.net/uestcyao/article/details/7874817 #读取图片并存储为矩阵 from scipy.misc import imread im = ...

  7. dubbo + zookeeper 配置

      Dubbo与Zookeeper.SpringMVC整合和使用 windows环境介绍: myeclipse 10 jdk1.6 tomcat 6.0.35 一.安装Zookeeper 1.通过链接 ...

  8. Java代码工具箱之控制台输出重定向_控制台输出到文件

    1. 情形:有时候控制台输出太多,在MyEclipse显示不全. 2. 说明:本代码只会重定向 system.out 的内容, error及其它不受代码影响.其它应该类似. //自定导入必要库 //然 ...

  9. 阿里云服务器下安装LAMP环境(CentOS Linux 6.3)(1)

    阿里的云服务器准备好以后,我们首先要做的就是把自己购买的磁盘空间挂载到系统里面,我们为服务器选择的是 Linux 系统,确切说的是 CentOS 系统. 默认阿里云服务器带了一个 20G 的空间,一般 ...

  10. 搜狗浏览器特性页面JS

    http://ie.sogou.com/features4.2.html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN ...