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. SpringCloud学习笔记《---01 概念 ---》篇

  2. linux 2.6 内核的移植

    内核移植过程   下载 linux 内核 从 http://www.kernel.org/pub/linux/kernel/v2.6/linux­2.6.14.1.tar.bz2 下载 linux­2 ...

  3. (转) Vultr能Ping但是SSH无法连接

    原文链接:https://www.bestqliang.com/2018/06/27/Vultr%E8%83%BDPing%E4%BD%86%E6%98%AFSSH%E6%97%A0%E6%B3%95 ...

  4. 【转载】TCP演进简述

    TCP演进简述 http://www.cnblogs.com/fll/ 一.互联网概述 TCP,即传输控制协议,是目前网络上使用的最多的传输协议,我们知道,整个互联网的体系结构是以IP协议提供的无连接 ...

  5. php中$_REQUEST、 $_GET、 $_POST、 $_COOKIE 的关系和区别

    看到REQUEST可以通吃GET .POST .COOKIE 后 感觉这个$_REQUEST太强大了是不是其他的几个超级变量就没有用了,下面对他们整体做个比较: 1.安全性 post>get 2 ...

  6. PAT甲级——【牛客练习A1004】

    题目描述 An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For ex ...

  7. C# 把十六进制表示的ASCII码转换为对应的字符组成的字符串

    0x30表示字符‘0’的ASCII码.

  8. 阿里云服务器(一)——Nodejs环境配置

    最近在阿里云上买了一个轻量应用服务器,想着用来学习一下Nodejs. 64位 配置Nodejs环境: 参考:https://www.runoob.com/nodejs/nodejs-install-s ...

  9. js 面向对象类

    类的声明 继承的几种方法 类的声明 第一种 function car(){ this.name = 'name'; } 第二种.es6中新添加的 class car(){ constructor(){ ...

  10. Activiti流程图部署及流程图部分操作

    流程图部署有两种方式,一种是通过classpath,另一种是通过zip文件 通过classpath方式如下 public void deploymentProcessDefinition_classp ...