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" 对于一些字符串类型较长的字段搜索时, 可以参考如下方法
随机推荐
- 比特币中P2PKH(pay-to-public-key-hash)的锁定脚本和解锁脚本
脚本格式 P2PKH的锁定脚本为: OP_DUP OP_HASH160 PUSHDATA(<Cafe Public Key Hash>) OP_EQUALVERIFY OP_CHECKSI ...
- Q9400为何难以100%全速运行
采用基于正域的约简. 数据:Ticdata2000 记录数:5822 条件属性:85 结果: 1. Core i7 3632QM 4四核八线程 2.2G 动态加速3.2G 0.516s 2. Core ...
- Flutter Json序列号和反序列化遇到问题 Missing "part 'xxx.g.dart';"
/** * * 1.@JsonSerializable() 这是表示告诉编译器这个类是需要生成Model类的 * 2,@JsonKey 由于服务器返回的部分数据名称在Dart语言中是不被允许的, * ...
- 问题 B: Curriculum Vitae
问题 B: Curriculum Vitae 时间限制: 1 Sec 内存限制: 128 MB提交: 109 解决: 25[提交][状态][讨论版][命题人:acm4302] 题目描述 Hideo ...
- C#箴言之用属性来访问类的私有成员
在程序中,难免要访问某个对象的私有成员.那么以前实现这类功能的方法有两种,第一种方法最简单,就是把成员访问符从“private”改为“public”即可:而另一个就是提供公有的成员访问函数来进行访问. ...
- WebAppBuilder独立于portal之arcgis for js应用框架研究之二
WAB采用ArcGIS JavaScript for API作为地图开发底层,采用Web AppBuilder作为开发框架,利用该框架即拿即用的Widget来构建应用,比如制图.查询.地理处理.编辑. ...
- 第30题:LeetCode155. Min Stack最小栈
设计一个支持 push,pop,top 操作,并能在O(1)时间内检索到最小元素的栈. push(x) -- 将元素 x 推入栈中. pop() -- 删除栈顶的元素. top() -- 获取栈顶元素 ...
- windows 时间同步至最新时间方法 | windows 时间同步服务器
国内 windows 系统的电脑有时候不能自动同步互联网当前时间,这就需要改一下 windows 的时间同步服务器 版权声明:本文为博主原创文章,未经博主允许不得转载. 原文地址:https://ww ...
- Python 正则表达式 利用括号分组
如果想把区号从匹配的电话号码中分离,可以添加括号在正则表达式中创建分组,再使用group()方法,从一个分组中获取匹配的文本 正则表达式字符串中,第一个括号是第一组,第二个括号是第二组.向group( ...
- 绘制圆形 和 椭圆形:边圆形 imageellipse() 、 填充圆形imagefilledellipse()
<?php //1. 绘制图像资源(创建一个画布) $image = imagecreatetruecolor(500, 300); //2. 先分配一个绿色 $green = imagecol ...