Play on Words[HDU1116]
Play on Words
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4094 Accepted Submission(s): 1328
Problem Description
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
3
2
acm
ibm
3
acm
malform
mouse
2
ok
ok
Sample Output
The door cannot be opened.
Ordering is possible.
The door cannot be opened.
Source
Central Europe 1999
Recommend
Eddy
Take letters 'a'..'z' as the nodes in graph.A word start with charcateristic c1 and end by characteristic c2 forms an edge in the graph.Then the problem become if there exist an Euler circuit.
So how can we judge whether a graph exists an Euler circuit or not ? First the graph should be a connected graph.We can determine this by breadth-first search.Then we have to check the in-degree and out-degree.Only two or zero if them can be different.If there are two difference,one of the two its in-degree minus its out-degree should be one while the other should be minus one.
#include<stdio.h>
#include<string.h>
#include<queue>
#include<iostream>
using namespace std;
int dr[26],dc[26],N,f[26];
bool e[26],g[26][26];
bool bfs()
{
int i;
memset(f,0,sizeof(f));
queue<int> q;
while (!q.empty()) q.pop();
for (i=0;i<26;i++)
if (e[i])
{
f[i]=true;
q.push(i);
break;
}
while (!q.empty())
{
int x=q.front();
q.pop();
for (i=0;i<26;i++)
if (g[x][i])
{
if (!f[i])
{
f[i]=true;
q.push(i);
}
}
}
for (i=0;i<26;i++)
if ((e[i]) && (!f[i])) return false;
return true;
}
int main()
{
int T,i;
scanf("%d",&T);
while (T--)
{
scanf("%d",&N);
char s[1024];
memset(g,0,sizeof(g));
memset(e,0,sizeof(e));
memset(dr,0,sizeof(dr));
memset(dc,0,sizeof(dc));
for (i=1;i<=N;i++)
{
scanf("%s",s);
int u=s[0]-'a',v=s[strlen(s)-1]-'a';
g[u][v]=true;
g[v][u]=true;
e[u]=true;
e[v]=true;
dc[u]++;
dr[v]++;
}
bool flag=bfs();
int cnt0=0,cnt1=0;
for (i=0;i<26;i++)
{
if (dr[i]-dc[i]>1 || dr[i]-dc[i]<-1) flag=false;
if (dr[i]-dc[i]==1) cnt0++;
if (dr[i]-dc[i]==-1) cnt1++;
}
if (!((cnt0==0 && cnt1==0)||(cnt0==1 && cnt1==1))) flag=false;
if (flag) printf("Ordering is possible.\n");
else printf("The door cannot be opened.\n");
}
return 0;
}
Play on Words[HDU1116]的更多相关文章
- HDU1116 Play on Words(有向图欧拉通路)
我把单词当作点,然后这样其实是不对的,这样就要判定是否是哈密顿通路.. 这题应该把单词的首尾单词当作点,而单词本身就是边,那样就是判定欧拉通路了. 有向图包含欧拉通路的充要条件是:首先基图连通,然后是 ...
- hdu1116 欧拉回路
//Accepted 248 KB 125 ms //欧拉回路 //以26个字母为定点,一个单词为从首字母到末尾字母的一条边 //下面就是有向图判断欧拉回路 //连通+节点入度和==出度和 或者 存在 ...
- HDU1116图论
http://acm.split.hdu.edu.cn/showproblem.php?pid=1116 #include<stdio.h> #include<algorithm&g ...
- hdu1116
http://acm.hdu.edu.cn/showproblem.php?pid=1116 #include<stdio.h> #include<math.h> #inclu ...
- hdu1116回溯N皇后问题
题目连接 经过思考,不难发现:恰好N个皇后放在不同行不同列,那么是不是可以转换成N个皇后所在行分别确定(一人一行)的情况下对她们的所在列的枚举. 也就是列的全排列生成问题,我们用c[x]表示x行皇后的 ...
- hdu1116有向图判断欧拉通路判断
Play on Words Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- hdu-1116(欧拉回路+并查集)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1116 思路:将字符串的头元素和尾元素视为图的x,y节点,然后合并x,y. 如果这个图不连通,则门不能打 ...
- HDU1116(欧拉路径+并查集)
题意: 给出一些字符串,有这两个字符串,如果第一个字符串的最后一个字母和第二个字符串的第一个字母是一样的,则这两个字符串是可以连接在一起的. 问给出的这些字符串能否串成一个环或者一整个链. 思路: 将 ...
- hdu1116 Play on Words--并查集
原题链接: pid=1116">http://acm.hdu.edu.cn/showproblem.php? pid=1116 一:原题内容 Problem Description S ...
随机推荐
- Powershell常用命令
Powershell常用命令1.Get-Command 得到Powshell所有命令2.Get-Process 获取所有进程3.Set-Alias 给指定命令重命名 如:Set-Alias aaa G ...
- 2015安徽省赛 D.锐雯上单不给就送
题目描述 <英雄联盟>(简称LOL)是由美国Riot Games开发,腾讯游戏运营的英雄对战网游.<英雄联盟>除了即时战略.团队作战外,还拥有特色的英雄.自动匹配的战网平台,包 ...
- Linux 系统安全 抵御TCP的洪水
抵御TCP的洪水 分类: LINUX tcp_syn_retries :INTEGER默认值是5对 于一个新建连接,内核要发送多少个 SYN 连接请求才决定放弃.不应该大于255,默认值是5,对应于1 ...
- 安装完mysql后用root不能su成mysql
现象: debian的机器用aptitude install mysql-server-5.1 后,用id mysql 可看出已经建了mysql用户,但是用root来su mysql 不成功,/var ...
- iOS 中NSOperationQueue,Grand Central Dispatch , Thread的上下关系和区别
In OS X v10.6 and later, operation queues use the libdispatch library (also known as Grand Central D ...
- PHP exit() 输出
2014年8月6日 10:41:00 exit($a); 当$a是bool类型,整形的时候,浏览器里是看不到任何输出的 当$a是字符串的时候浏览器里是可以看到输出的 $a = 1; exit($a); ...
- 利用FFmpeg生成视频缩略图 2.1.8
1.下载FFmpeg文件包,解压包里的\bin\下的文件解压到 D:\ffmpeg\ 目录下. 下载地址 http://ffmpeg.zeranoe.com/builds/win32/static/ ...
- DP:Islands and Bridges(POJ 2288)
2015-09-21 造桥基建工程 题目大意,就是有n座岛和k座桥,要你找一条哈密顿圈(找完所有的岛,并且每个岛只经过一次),当经过一座岛就加上岛的价值,如果两岛联通,则加上两座岛的价值之积,如果三座 ...
- Live Archive 3644 X-Plosives 解题报告
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=1 ...
- HTML5学习记录1-新特性
新特性 HTML5 中的一些有趣的新特性: 1. 用于绘画的 canvas 元素 2. 用于媒介回放的 video 和 audio 元素 3. 对本地离线存储的更好的支持 4. 新的特殊内容元素,比如 ...