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啦!!!)的更多相关文章

  1. uva 10129 poj 1386 hdu 1116 zoj 2016 play on words

    //本来是想练一下欧拉回路的,结果紫书上那题是大水题!!!!! 题意:给出n个单词,是否可以把单词排列成每个单词的第一个字母和上一个单词的最后一个字母相同 解:欧拉通路存在=底图联通+初度!=入度的点 ...

  2. POJ 2104&HDU 2665 Kth number(主席树入门+离散化)

    K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 50247   Accepted: 17101 Ca ...

  3. POJ 1386 Play on Words(欧拉图的判断)

    Play on Words Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11838   Accepted: 4048 De ...

  4. 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 ...

  5. Play on Words HDU - 1116 (并查集 + 欧拉通路)

    Play on Words HDU - 1116 Some of the secret doors contain a very interesting word puzzle. The team o ...

  6. Eight POJ - 1077 HDU - 1043 八数码

    Eight POJ - 1077 HDU - 1043 八数码问题.用hash(康托展开)判重 bfs(TLE) #include<cstdio> #include<iostream ...

  7. POJ 1177/HDU 1828 picture 线段树+离散化+扫描线 轮廓周长计算

    求n个图矩形放下来,有的重合有些重合一部分有些没重合,求最后总的不规则图型的轮廓长度. 我的做法是对x进行一遍扫描线,再对y做一遍同样的扫描线,相加即可.因为最后的轮廓必定是由不重合的线段长度组成的, ...

  8. HDU 1116 || POJ 1386 || ZOJ 2016 Play on Words (欧拉回路+并查集)

    题目链接 题意 : 有很多门,每个门上有很多磁盘,每个盘上一个单词,必须重新排列磁盘使得每个单词的第一个字母与前一个单词的最后一个字母相同.给你一组单词问能不能排成上述形式. 思路 :把每个单词看成有 ...

  9. poj和hdu部分基础算法分类及难度排序

    最近想从头开始刷点基础些的题,正好有个网站有关于各大oj的题目分类(http://www.pythontip.com/acm/problemCategory),所以写了点脚本把hdu和poj的一些题目 ...

随机推荐

  1. Java中循环体的初步了解以及另一种随机数的获取方法

    Math中的相关操作 随机数 Java中除了可以直接导入Random类,获取随机数,还可以通过本身自带的Math方法去获取随机数.Math.random()可以产生随机小数,区间范围为[0.0,1.0 ...

  2. 【LGP5349】幂

    题目 比较厉害的题目了 求 \[\sum_{i=0}^{\infty}\sum_{j=0}^nf_ji^jr^i\] 改变一下求和顺序 \[\sum_{j=0}f_j\sum_{i=0}^{\inft ...

  3. JS流程控制语句 做判断(if语句)if语句是基于条件成立才执行相应代码时使用的语句。语法:if(条件) { 条件成立时执行代码}

    做判断(if语句) if语句是基于条件成立才执行相应代码时使用的语句. 语法: if(条件) { 条件成立时执行代码} 注意:if小写,大写字母(IF)会出错! 假设你应聘web前端技术开发岗位,如果 ...

  4. Android开发 音视频开发需要了解的专业术语知识

    前言 在摸索一段时间的音视频开发后,越来越发现这个坑的深度真是特别的深. 除了了解Android自带的音视频处理API以外,还得了解一些视频与音频方面的知识.这篇博客就是主要讲解这方面的专业术语.内容 ...

  5. Android开发 处理拍照完成后的照片角度

    private void initImageAngle(){ Bitmap imageBitmap = BitmapFactory.decodeFile(FilePathSession.getFace ...

  6. [转]Entity Framework教程(第二版)

    源起 很多年前刚毕业那阵写过一篇关于Entity Framework的文章,没发首页却得到100+的推荐.可能是当时Entity Framework刚刚发布介绍EF的文章比较少.一晃这么多年过去了,E ...

  7. 廖雪峰Java14Java操作XML和JSON-1XML-4第三方XML库

    总结: 使用Jackson可以快速在XML和JavaBean之间互相转换 可使用Annotation定制序列化和反序列化

  8. ActiveMQ 基础

    Apache ActiveMQ是Apache软件基金会所研发的开放源代码消息中间件:由于ActiveMQ是一个纯Java程序,因此只需要操作系统支持Java虚拟机,ActiveMQ便可执行.它能很好地 ...

  9. 暑期集训日志(Day0~Day5)

    章·五:2019-07-15:明月不谙离恨苦,斜光到晓穿朱户 ·昨日小结: 昨天考试又是爆零边缘,除了难过就剩难过了. T1暴力打崩了只拿了5分. T2没给分时间.最后20分钟打了个残码.没仔细观察数 ...

  10. 莫烦PyTorch学习笔记(五)——模型的存取

    import torch from torch.autograd import Variable import matplotlib.pyplot as plt torch.manual_seed() ...