poj1386 字符串类型的一笔画问题 欧拉回路
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 10685 | Accepted: 3625 |
Description
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
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
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 字符串类型的一笔画问题 欧拉回路的更多相关文章
- StackExchange.Redis帮助类解决方案RedisRepository封装(字符串类型数据操作)
本文版权归博客园和作者本人共同所有,转载和爬虫请注明原文链接 http://www.cnblogs.com/tdws/tag/NoSql/ 目录 一.基础配置封装 二.String字符串类型数据操作封 ...
- Redis命令拾遗一(字符串类型)
文章归博客园和作者“蜗牛”共同所有 .转载和爬虫请注明原文Redis系列链接 http://www.cnblogs.com/tdws/tag/NoSql/ Redis有五种基本数据类型.他们分别是字符 ...
- Java中,关于字符串类型、随机验证码、 时间类型
一.字符串类型:String类型 定义一个字符串 String a="Hello World"; String b= new String ("Hello World&q ...
- 学习笔记:MySQL字符串类型
字符串类型 a) char和varchar 1.都需要指定字符的长度,char中的长度是字符的长度,而varchar的长度是字节的长度 2. char中指定的长度就是实际占用的长度,而 ...
- Redis常用命令入门1:字符串类型命令
Redis总共有五种数据类型,在学习的时候,一定要开一个redis-cli程序,边看边练,提高效率. 一.最简单的命令 1.获得符合规则的键名列表 keys * 这里的*号,是指列出所有的键,同时*号 ...
- Spark1.3使用外部数据源时条件过滤只要是字符串类型的值均报错
CREATE TEMPORARY TABLE spark_tbls USING org.apache.spark.sql.jdbc OPTIONS ( url 'jdbc:mysql://hadoop ...
- Redis从基础命令到实战之字符串类型
字符串类型是Redis中最基本的数据类型,能存储任何形式的字符串和和二进制数据.本文以代码形式列举常用的操作命令,并在实践部分演示一个简单的商品管理功能,实现了通常使用关系型数据库开发的增改查功能,注 ...
- 后台返回字符串类型function的处理 (递归算法)
$(function(){ $.ajax({ type: "post", url: "${ctx}/modules/fos/reference/echart", ...
- mysql中的字符串类型数据索引优化
摘自 "高性能mysql" 对于一些字符串类型较长的字段搜索时, 可以参考如下方法
随机推荐
- spa 小程序的研发随笔 (2) --- 预编译
因为是连续写的2篇随笔,废话不多说.直接进入正题. 选择预编译的工具时,笔者采用了gulp.虽然,如今市面上大多采用的多为webpack,使用gulp也是有自己的缘由的. webpack的最主要特点是 ...
- MyBatis的discriminator鉴别器根据字段值实现Java中的多态
<select id="getModelById" resultMap="modelTypeMap"> SELECT id as id, model ...
- CUDA:Supercomputing for the Masses (用于大量数据的超级计算)-第一节
原文链接 第一节 CUDA 让你可以一边使用熟悉的编程概念,一边开发可在GPU上运行的软件. Rob Farber 是西北太平洋国家实验室(Pacific Northwest National Lab ...
- 51nod——1640 天气晴朗的魔法 有边权限制的最大生成树
好好读题嗷:“所以我们要求阵中的魔法链的魔力值最大值尽可能的小,与此同时,魔力值之和要尽可能的大.” 第一条件是生成树的最大边权更小,第二条件是在最大边权的限制下搞一个最大生成树. 至于最大生成树,如 ...
- SummerVocation_Learning--java的线程机制
线程:是一个程序内部的执行路径.普通程序只有main()一条路径.如下列程序: import java.lang.Thread; //导入线程实现包 public class Test_Thread ...
- org.hibernate.hql.internal.ast.QuerysyntaxException:user is not mapped [from User where user_code=? and user_password=?]
初学者,一个很低级的错误吧! 找不到映射,最后发现没把类的Hibernate映射文件 添加到Hibernate核心配置文件中去,所以报了这个异常! 在核心文件中添加映射 <mapping r ...
- struts2、hibernate和SSH的实现
Struts2 为什么开发Struts框架? 为了符合更加灵活.高效的开发需求 实质上Struts2是以WebWork为核心的,他采用拦截机制来处理用户请求. (1)Jsp部分 <%@ page ...
- restful api 规范
- 使用paramiko报错:CryptographyDeprecationWarning: Support for unsafe construction of public numbers from encoded data will be removed in a future version. Please use EllipticCurvePublicKey.from_encoded_poi
1.paramiko不支持cryptography-2.6.1 pip3 uninstall cryptography==2.6.1 2.paramiko 支持cryptography-2.4.2 p ...
- JZOJ 5818. 【NOIP提高A组模拟2018.8.15】 做运动
5818. [NOIP提高A组模拟2018.8.15] 做运动 (File IO): input:running.in output:running.out Time Limits: 2000 ms ...