Play on Words
Description
Some of the secret doorscontain a very interesting word puzzle. The team of archaeologists has to solveit to open that doors. Because there is no other way to open the doors, thepuzzle is very important for us.
There is a large number of magnetic plates on every door. Every plate has oneword written on it. The plates must be arranged into a sequence in such a waythat every word begins with the same letter as the previous word ends. Forexample, the word ``acm'' can
be followed by the word ``motorola''. Your taskis to write a computer program that will read the list of words and determinewhether it is possible to arrange all of the plates in a sequence (according tothe given rule) and consequently to open the door.
Input
The input consists of T testcases. 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 Nthatindicates the number of plates (1 <= N <= 100000). Then exactly
Nlinesfollow, each containing a single word. Each word contains at least two and atmost 1000 lowercase characters, that means only letters 'a' through 'z' willappear in the word. The same word may appear several times in the list.
Output
Your program has to determinewhether it is possible to arrange all the plates in a sequence such that thefirst letter of each word is equal to the last letter of the previous word. Allthe plates from the list must be used, each exactly once.
The words mentionedseveral times must be used that number of times.
If there exists such an ordering of plates, your program should print thesentence "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
CentralEurope 1999
解题思路:
这是一个欧拉路的题目,求这种欧拉路径实现要先根据输入的第一个字母和最后一个字母来建图,然后用并查集来判断图的连通性(也可以用bfs),如果连通,在根据出度和入度判断地方是欧拉路,如果是就可以直接输出
代码:
#include <iostream>
#include<stdio.h>
#include<string.h>
#define MAX 27
using namespace std;
int g[MAX];
int indegree[MAX];
int outdegree[MAX];
int set[MAX];
void init()
{
for(int i=0;i<MAX;i++)
{
set[i]=i;
}
}
int findSet(int x)
{
if(x!=set[x])
set[x]=findSet(set[x]);
return set[x];
}
void unionSet(int x,int y)
{
int a=findSet(x);
int b=findSet(y);
if(a==b)
return;
if(set[a]<set[b])
{
set[b]=a;
}
else
{
set[a]=b;
}
}
int main()
{
int ts;
int i;
scanf("%d",&ts);
while(ts--)
{
int num;
memset(g,0,sizeof(g));
memset(indegree,0,sizeof(indegree));
memset(outdegree,0,sizeof(outdegree));
scanf("%d",&num);
init();
for(i=0;i<num;i++)
{
char ch[1002];
scanf("%s",ch);
//gets(ch);
int len=strlen(ch);
int s=ch[0]-'a';
int e=ch[len-1]-'a';
g[s]=1;
g[e]=1;
//点s和点e的度数自加
indegree[e]++;
outdegree[s]++;
//将点s和e连接起来
unionSet(s,e);
}
int count1=0,count2=0;
int flag=0;
//判断图是否连通——变量一遍所有节点,如果每一个节点的父亲都是相同的,那么图是连通的
for(i=0;i<MAX;i++)
{/*
if(g[i]==1)
{
count++;
if(flag==0)
{
temp=findSet(i);
flag=1;
}
else
{
if(temp!=findSet(i))
{
break;
}
}
}
*/
//如果超过一个点的父亲是他本身,那么表示不连通
if(g[i]==1 && set[i]==i)
{
flag++;
}
}
if(flag>1)//图不连通
{
printf("The door cannot be opened.\n");
continue;
}
//如果图连通,判读是否是欧拉路径
/*无向图中,除源点和汇点为奇数除外,其余点的度数都为偶数,或者所有点的度数为偶数,那么存在欧拉路径*/
for(i=0;i<MAX;i++)
{
if(g[i]==1)
{
if((indegree[i]!=outdegree[i]))
{
//如果出现OK Ok这样一个词,那么就要判断如果出度减去入度大于1的话就不符合
if((indegree[i]-outdegree[i])==1)
count1++;
else if((outdegree[i]-indegree[i])==1)
count2++;
else
break;
}
}
}
if(i<MAX)
printf("The door cannot be opened.\n");
else if((count1+count2)==0 || (count1==1 && count2==1))
printf("Ordering is possible.\n");
else
printf("The door cannot be opened.\n");
}
return 0;
}
分享到:
相关推荐
2遍dp poj_3613解题报告 poj_3613解题报告
poj题目2775文件子目录源代码,递归经典题目,
poj典型题目解题思路详解 包含源代码和解题时应注意的问题及题目陷阱设计分析
poj 1699的代码和方法说明,个人原创
C_(POJ_1854)(分治).cpp
poj上第1990题目源码,用到了2个树状数组,这题数据结构是关键,想到了题目就很简单了
D_(POJ_1723)(思维)(sort).cpp
原理为:以数链思想,移动数组中的内容 使数组在没有扩充情况下,达成移动的效果 当然,有更简单的,大牛不要笑哦
D_(POJ_1723)(思维)(第k大数).cpp
POJ 3131 双向BFS解立体八数码问题
O(nlogn)凸包问题 poj2187
poj两道题的c++实现。已经测试过可以通过oj
POJ题目及算法,包括动态规划、深搜广搜等算法。含相关注释。
这是北大在线测试的第1002题,方便记忆的电话号码的解题例程,题目中有一个列表,记录着许多方便记忆的电话号码。不同的方便记忆的电话号码可能对应相同的标准号码,这个程序的任务就是找到它们
问题:求平面上多个矩形的总面积。 算法:线段树(经典的线段树题目)
poj 3310 的代码和方法说明,个人原创
http://poj.grids.cn/problem?id=2774 POJ 2774 木棒加工 木材厂有一些原木,现在想把这些木头切割成一些长度相同的小段木头,需要得到的小段的数目是给定了。当然,我们希望得到的小段越长越好,你的任务是计算能够...
看了测试用例后,大家估计已经明白题目的意思了吧!题目很简单,但是就是很烦。 开始直接是没思路,不知道怎么模拟,但是想了带该半个小时,就搞定了。就是把每个数存到数组里面。
POJ上面题目的解题报告。涵盖挺多的。可作参考。代码都正确。ACM新手入门必下~~ 加油...
三道几何题:hdu 1007、hdu 2289、poj 3714