POJ 1386&&HDU 1116 Play on Words(我以后再也不用cin啦!!!)
Play on Words
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
acm
ibm acm
malform
mouse ok
ok
Sample Output
The door cannot be opened.
Ordering is possible.
The door cannot be opened.
HDU timelimit 5000ms
POJ timelimit 1000ms emmmmmm~
然后这题用cin巨坑啊!!!浪费了我两个小时...
首先很显然是一道求欧拉路的问题,开始我用邻接表+dfs在POJ上做的,交了一发TLE...emmmm。后面才发现总共就26个点,于是换邻接矩阵,又交一发,TLEx2...emmmmm。后思考许久,没想到怎么优化,然后搜到HDU也有一道相同的题,但limit5000ms,遂在HDU交一发..1150ms左右,这离1000ms很近啊?开始我还是有点怀疑cin是不是慢了,但想着怎么着也不会慢这么多吧?
遂在网上找了另一种做法并查集,hoho~这个看起来要快一些,生搬硬套一番别人的代码(我为了图方便还是用的cin输入),POJ上交一发...果不其然TLE,我(哔......)...
没办法,最后把string换成char数组,cin换成scanf,dfs交一发 320+ms,,,并查集交一发310+ms....我......我以后再也不用cin啦jojo!!
/**
* time: 320ms
* 邻接矩阵dfs求欧拉路
*/
#include <iostream>
#include <cstdio>
#include <string.h>
#include <algorithm>
#define MIN(x,y) ((x)>(y))?(y):(x)
#define MAX(x,y) ((x)>(y))?(x):(y) using namespace std; const int inf = 0x3f3f3f3f;
const double dinf = 0xffffffff;
const int vspot = ;
const int espot = ;
const int qspot = ; int indeg[vspot], outdeg[vspot];
int g[vspot][vspot];
int N, cnt, k; void init()
{
cnt = k = ;
memset( g, , sizeof(g) );
memset( indeg, , sizeof(indeg) );
memset( outdeg, , sizeof(outdeg) );
} int read()
{
scanf( "%d", &N );
int x, y, start;
char str[];
for( int i = ; i < N; i++ )
{
scanf( "%s", str ); //别用cin...
int len = strlen(str); x = str[] - 'a';
y = str[len-] - 'a'; g[x][y]++;
indeg[y]++;
outdeg[x]++;
start = MIN(x,y); //初始搜索点须是出现过的点,不用MIN也行直接start=x
} return start;
} int dfs( int x )
{
for( int i = ; i < ; i++ )
if ( g[x][i] )
{
g[x][i]--;
dfs(i);
k++;
}
return k;
} int main()
{
int all;
cin >> all;
while( all-- )
{
init();
int start = read(); int test = , cne = , cns = ; //cns表示满足入度等于出度-1的点 个数
for( int i = ; i < ; i++ ) //cne表示满足入度等于出度+1的点 个数
if ( indeg[i] != outdeg[i] )
{
if ( indeg[i] == outdeg[i] + )
cne++;
else if ( indeg[i] == outdeg[i] - )
{ cns++; start = i; }
else
test++;
} if ( test )
cout << "The door cannot be opened." << endl;
else if ( !((cns==&&cne==) || (cns==&&cne==)) )
cout << "The door cannot be opened." << endl;
else
{
test = dfs(start); if ( test == N )
cout << "Ordering is possible." << endl;
else
cout << "The door cannot be opened." << endl; //若dfs搜不完全图说明不连通
}
} return ;
}
/**
* time: 320ms
* 并查集
*/
#include <iostream>
#include <cstdio>
#include <string.h>
#include <algorithm>
#define MIN(x,y) ((x)>(y))?(y):(x)
#define MAX(x,y) ((x)>(y))?(x):(y) using namespace std; const int inf = 0x3f3f3f3f;
const double dinf = 0xffffffff;
const int vspot = ;
const int espot = ;
const int qspot = ; int root[vspot], indeg[vspot], outdeg[vspot];
int N;
bool vis[vspot]; int find( int x )
{
return x == root[x] ? x : root[x]=find(root[x]); //路径压缩其实没必要..毕竟就26个点
} void unions( int x, int y )
{
int x1, x2;
x1 = find(x);
x2 = find(y);
if(x1 != x2)
root[x2] = x1;
} void init()
{
memset( indeg, , sizeof(indeg) );
memset( outdeg, , sizeof(outdeg) );
memset( vis, false, sizeof(vis) );
for( int i = ; i < vspot; i++ )
root[i] = i;
} int main()
{
int all;
cin >> all;
while( all-- )
{
init();
scanf( "%d", &N );
char str[];
int x, y;
for( int i = ; i < N; i++ )
{
scanf( "%s", str );
int len = strlen(str);
x = str[] - 'a';
y = str[len-] - 'a'; vis[x] = vis[y] = true;
indeg[y]++;
outdeg[x]++;
unions(x,y);
} int test = , cne = , cns = ;
for( int i = ; i < ; i++ )
if ( indeg[i] != outdeg[i] ) //测试方法和前面的代码一样
{
if ( indeg[i] == outdeg[i] + )
cne++;
else if ( indeg[i] == outdeg[i] - )
cns++;
else
test++;
} if ( test )
cout << "The door cannot be opened." << endl;
else if ( !((cns==&&cne==) || (cns==&&cne==)) )
cout << "The door cannot be opened." << endl;
else
{
test = -;
int k = ;
for( int i = ; i < ; i++ )
if ( vis[i] )
if ( root[i] == i ) //换成test!=root[i]就不对??why??
k++;
if ( k == )
cout << "Ordering is possible." << endl;
else
cout << "The door cannot be opened." << endl;
}
} return ;
}
POJ 1386&&HDU 1116 Play on Words(我以后再也不用cin啦!!!)的更多相关文章
- uva 10129 poj 1386 hdu 1116 zoj 2016 play on words
//本来是想练一下欧拉回路的,结果紫书上那题是大水题!!!!! 题意:给出n个单词,是否可以把单词排列成每个单词的第一个字母和上一个单词的最后一个字母相同 解:欧拉通路存在=底图联通+初度!=入度的点 ...
- POJ 2104&HDU 2665 Kth number(主席树入门+离散化)
K-th Number Time Limit: 20000MS Memory Limit: 65536K Total Submissions: 50247 Accepted: 17101 Ca ...
- POJ 1386 Play on Words(欧拉图的判断)
Play on Words Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 11838 Accepted: 4048 De ...
- poj 1251 poj 1258 hdu 1863 poj 1287 poj 2421 hdu 1233 最小生成树模板题
poj 1251 && hdu 1301 Sample Input 9 //n 结点数A 2 B 12 I 25B 3 C 10 H 40 I 8C 2 D 18 G 55D 1 E ...
- Play on Words HDU - 1116 (并查集 + 欧拉通路)
Play on Words HDU - 1116 Some of the secret doors contain a very interesting word puzzle. The team o ...
- Eight POJ - 1077 HDU - 1043 八数码
Eight POJ - 1077 HDU - 1043 八数码问题.用hash(康托展开)判重 bfs(TLE) #include<cstdio> #include<iostream ...
- POJ 1177/HDU 1828 picture 线段树+离散化+扫描线 轮廓周长计算
求n个图矩形放下来,有的重合有些重合一部分有些没重合,求最后总的不规则图型的轮廓长度. 我的做法是对x进行一遍扫描线,再对y做一遍同样的扫描线,相加即可.因为最后的轮廓必定是由不重合的线段长度组成的, ...
- HDU 1116 || POJ 1386 || ZOJ 2016 Play on Words (欧拉回路+并查集)
题目链接 题意 : 有很多门,每个门上有很多磁盘,每个盘上一个单词,必须重新排列磁盘使得每个单词的第一个字母与前一个单词的最后一个字母相同.给你一组单词问能不能排成上述形式. 思路 :把每个单词看成有 ...
- poj和hdu部分基础算法分类及难度排序
最近想从头开始刷点基础些的题,正好有个网站有关于各大oj的题目分类(http://www.pythontip.com/acm/problemCategory),所以写了点脚本把hdu和poj的一些题目 ...
随机推荐
- springboot-配置多数据源(AOP实现)(HikariCP + MybatisPlus + mysql + SqlServer)
场景: springboot项目,默认使用HikariCP连接池 + MybatisPlus持久层框架 + mysql数据库等一套流程,现需求需去第三方sqlserver数据库拉取数据,直连数据库,不 ...
- 华为-eNSP模拟器路由器无法正常启动一直显示“#”
问题项如截图: 解决方案: 1. 打开自己电脑的控制面板 -->> 系统和安全 -->> Windows Defender防火墙 (运行应用通过Windows防火墙) 2 .找 ...
- nodejs和vuejs的关系
转自:https://blog.csdn.net/myKurt/article/details/79914078 nodejs类比Java中:JVM 详述: 就前端来说nodejs具有划时代的意义, ...
- 【左偏树】 [JLOI2015]城池攻占
原来左偏树还可以打tag,get了 和线段树打tag一样,时不时Push_Down就好了 然后这里显然也是要先乘法后加法的 tag打上了之后还是其他一般左偏树差不多,有些细节注意一下 然后开 long ...
- 使用openSUSE Leap 42.2小记
闪存记录 在2017年04月10日开始想用openSuSE. 2017年04月10日开始找资料制作U盘安装openSUSE. 是在windows 7中用imageWrite.exe 软件制作的.安装的 ...
- BZOJ 4765: 普通计算姬 (分块+树状数组)
传送门 解题思路 树上的分块题,,对于修改操作,每次修改只会对他父亲到根这条链上的元素有影响:对于查询操作,每次查询[l,r]内所有元素的子树,所以就考虑dfn序,进标记一次,出标记一次,然后子树就是 ...
- css过渡属性transition简单示例
2.transition 简单实例 demo1→在线预览源代码 效果 demo2→在线预览源代码 效果 demo3→在线预览源代码 效果
- 密码学笔记(2)——RSA密码
上一篇笔记中讲述了大量的代数知识,这一篇中我们看看如何将这些代数知识应用到RSA密码体制中. 一.公钥密码学简介 在经典密码学的研究模型中,我们根据已选择的秘钥K得到一条加密规则$e_{k}$和一条解 ...
- 2019-5-24-WPF-源代码-从零开始写一个-UI-框架
title author date CreateTime categories WPF 源代码 从零开始写一个 UI 框架 lindexi 2019-05-24 15:54:36 +0800 2018 ...
- Find- Linux必学的60个命令
1.作用 find命令的作用是在目录中搜索文件,它的使用权限是所有用户. 2.格式 find [path][options][expression] path指定目录路径,系统从这里开始沿着目录树向下 ...