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" 对于一些字符串类型较长的字段搜索时, 可以参考如下方法
随机推荐
- WMAppPRHeader.xml文件的作用
对于很多开发者来说直接通过Visual Studio 2010生成的XAP文件和上传至App Hub通过Windows Phone Marketplace Technical Certificatio ...
- void (*signal(int sig, void (*func) (int))) (int)理解
http://blog.csdn.net/sever2012/article/details/8281271 1.signal( int sig, void (*func)(int))signal是一 ...
- 【BZOJ1013】[JSOI2008] 球形空间产生器(高斯消元)
点此看题面 大致题意: 给定一个\(n\)维球体上的\(n+1\)个点,请你求出这个球体的圆心的位置. 列出方程 这一看就是一道解方程题. 我们可以设这个球体的圆心的位置为\((x_1,x_2,..x ...
- Restful API 的设计规范
RESTful 是目前最流行的 API 设计规范,用于 Web 数据接口的设计.降低开发的复杂性,提高系统的可伸缩性. Restful API接口规范包括以下部分: 一.协议 API与用户的通信协议, ...
- oracle用户创建及权限设置(转)
权限: create session create table unlimited tablespace connect resource dba 例: #sqlplus /nolog SQL> ...
- 操作系统(3)_CPU调度_李善平ppt
不只上面的四种,比如时间片到了也会引起调度. 具体的调度算法: fcfs简单,但是波动很大. 最高相应比算法,执行时间最长就应该等待的长点,比sjf多了一个等待时间的考虑. 硬件定时器和软件计数器共同 ...
- 1412: [ZJOI2009]狼和羊的故事
Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 4017 Solved: 2037[Submit][Status][Discuss] Descript ...
- MySQL - EXISTS 和 NOT EXISTS
语法规则: SELECT * FROM tableName t WHERE 1 = 1 AND 2 = 2 AND EXISTS (SELECT * FROM tableName t2 WHERE ...
- 网络编程-osi七层
一.操作系统基础 操作系统:(Operating System,简称OS)是管理和控制计算机硬件与软件资源的计算机程序,是直接运行在“裸机”上的最基本的系统软件,任何其他软件都必须在操作系统的支持下才 ...
- 09.VUE学习之watch监听属性变化实现类百度搜索栏功能ajax异步请求数据,返回字符串
cmd下安装axios npm install axios 安装好后,会多出node_modules文件夹 思路: 监听data里的word改变时,发送ajax异步请求数据, 把返回的数据赋值给dat ...