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的一些题目 ...
随机推荐
- Aria2 使用
没错,又是受够了迅雷.旋风的各种奇葩减速(哥哥我还买了了VIP!),IDM 对协议支持又太少还有事没事提示你不是正版三天两头闹着要更新.于是我想起来之前看到过的 Aria2,虽然之前也只是略有耳闻,但 ...
- It's a Mod, Mod, Mod, Mod World (类欧几里得模板题
https://vjudge.net/contest/317000#problem/F #include <iostream> #include <cstdio> #inclu ...
- MSSQLSERVER跨服务器连接(远程登录)的示例代码
MSSQLSERVER跨服务器链接服务器创建方法如下 复制代码 代码如下: --声明变量 Declare @svrname varchar(255), @dbname varchar(255), @s ...
- duilib教程之duilib入门简明教程1.前言
关于duilib的介绍就不多讲了,一来不熟,二来小伙伴们想必已经对比了多个界面库,也无需赘述.下面进入正题: 不看广告看疗效! 已有众多知名公司采用duilib做为界面库,如华为网盘.PPS(P ...
- 基于知识图谱的APT组织追踪治理
高级持续性威胁(APT)正日益成为针对政府和企业重要资产的不可忽视的网络空间重大威胁.由于APT攻击往往具有明确的攻击意图,并且其攻击手段具备极高的隐蔽性和潜伏性,传统的网络检测手段通常无法有效对其进 ...
- npm 安装vue 报错Failed at the chromedriver@2.34.0 install script 'node install.js'
提示版本不够,后来百度到,在你的vue项目包下执行: npm install chromedriver --chromedriver_cdnurl=http://cdn.npm.taobao.org/ ...
- 项目接入即时聊天客服系统(环信系统)PHP后端操作
环信工作原理: 一.由于环信没有直接的接口来主动调取本项目中的用户数据,所有用户信息必须在环信服务器上注册对应信息成为环信的用户:(这样才能当用户进入聊天时显示其基本信息,如:名称.昵称.电话.邮箱等 ...
- 101 删除排序数组中的重复数字 II
原题网址:http://www.lintcode.com/zh-cn/problem/remove-duplicates-from-sorted-array-ii/# 跟进“删除重复数字”: 如果可以 ...
- 用this 对方法的扩展
*都是静态方法 this指向的是调用的object*
- Redis学习笔记01-分布式锁
1.分布式锁的定义与理解 在并发任务中,当对数据执行修改和删除时为了防止多个任务同时拿到数据而产生的混乱,这时就要用到分布式锁来限制程序的并发执行. Redis分布式锁本质上要实现的目标就是在Redi ...