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. leetcode--5 Longest Palindromic Substring

    1. 题目: Given a string S, find the longest palindromic substring in S. You may assume that the maximu ...

  2. [转载]AngularJS入门教程04:双向绑定

    在这一步你会增加一个让用户控制手机列表显示顺序的特性.动态排序可以这样实现,添加一个新的模型属性,把它和迭代器集成起来,然后让数据绑定完成剩下的事情. 请重置工作目录: git checkout -f ...

  3. 反转链表[剑指offer]之python实现

    输入一个链表,输出反转后的链表. 非递归实现: # -*- coding:utf-8 -*- # class ListNode: # def __init__(self, x): # self.val ...

  4. 客户端(springmvc)调用netty构建的nio服务端,获得响应后返回页面(同步响应)

    后面考虑通过netty做一个真正意义的简约版RPC框架,今天先尝试通过正常调用逻辑调用netty构建的nio服务端并同步获得返回信息.为后面做铺垫 服务端实现 我们先完成服务端的逻辑,逻辑很简单,把客 ...

  5. 2018年ElasticSearch6.2.2教程ELK搭建日志采集分析系统(教程详情)

    章节一  2018年 ELK课程计划和效果演示1.课程安排和效果演示    简介:课程介绍和主要知识点说明,ES搜索接口演示,部署的ELK项目演示    es: localhost:9200    k ...

  6. 学习笔记(七): Logistic Regression

    目录 Calculating a Probability Model Training 1.Loss function for Logistic Regression 2.Regularization ...

  7. MySQL - GROUP_CONCAT 使用方法

    如上图,我想把结果集中的三行链接成一行,则可这样写:   总结: GROUP_CONCAT函数默认是用','逗号链接,如果你加上第二个参数,则以',第二个参数值'逗号+第二个参数值链接,如下图     ...

  8. 二十一、MySQL NULL 值处理

    MySQL NULL 值处理 我们已经知道 MySQL 使用 SQL SELECT 命令及 WHERE 子句来读取数据表中的数据,但是当提供的查询条件字段为 NULL 时,该命令可能就无法正常工作. ...

  9. PHP实现的敏感词过滤方法

    PHP实现的敏感词过滤方法,以下是一份过滤敏感词的编码.有需要可以参考参考. /** * @todo 敏感词过滤,返回结果 * @param array $list 定义敏感词一维数组 * @para ...

  10. 使用VUE开发

    <一>VUE的开发分两种,一种是直接在HTML文件中使用,一种是VUE文件的形式开发 1,首先我们先让 HTML 文件支持 VUE 的语法指令提示 2,File -> Setting ...