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. MongoDB特性及使用场景

    概述 MongoDB(Humongous Database),中文意思就是巨大无比的数据库,顾名思义,MongoDB就是为处理大数据而生,以解决海量数据的存储和高效查询使用为使命.MongoDB是一款 ...

  2. Activiti表单(Form key)

    1.设置Form key如图: 2.根据任务id得到Form key TaskFormData formData = formService.getTaskFormData(taskId);; Str ...

  3. 设置IDEA中properties文件显示中文

    路径: File - Setting - Editor - Code Style - File Encodings

  4. python中的多线程编程与暂停、播放音频的结合

    先给两个原文链接: https://blog.csdn.net/u013755307/article/details/19913655 https://www.cnblogs.com/scolia/p ...

  5. Mui本地打包笔记(一)使用AndroidStudio运行项目 转载 https://blog.csdn.net/baidu_32377671/article/details/79632411

    转载 https://blog.csdn.net/baidu_32377671/article/details/79632411 使用AndroidStudio运行HBuilder本地打包的Mui项目 ...

  6. PAT甲级——A1096 Consecutive Factors【20】

    Among all the factors of a positive integer N, there may exist several consecutive numbers. For exam ...

  7. 转载:python操作excel表格(xlrd/xlwt)

    python操作excel表格(xlrd/xlwt)   最近遇到一个情景,就是定期生成并发送服务器使用情况报表,按照不同维度统计,涉及python对excel的操作,上网搜罗了一番,大多大同小异,而 ...

  8. 一个WEB网站高并发量的解决方案

    一个小型的网站,可以使用最简单的html静态页面就实现了,配合一些图片达到美化效果,所有的页面均存放在一个目录下,这样的网站对系统架构.性能的要求都很简单.随着互联网业务的不断丰富,网站相关的技术经过 ...

  9. Liunx常用命令行(Ubuntu)

    关闭防火墙的命令行: 1. 永久性生效 开启:chkconfig iptables on 关闭:chkconfig iptables off 2. 即时生效,重启后失效 开启:service ipta ...

  10. iOS 更新日志 - 持续更新中

    本文只是为了简单记录一下每个正式版本发布时间和更新内容,只有这个初衷,从2019年9月25日开始,将会持续更新. iOS 13.1 - 2019年9月25日 iOS 13.1 iOS 13.1 包括错 ...