您的位置:首页 > 其它

poj 1386 Play on Words

2017-04-11 19:12 316 查看
Play on Words

Time Limit: 1000MS Memory Limit: 10000K

Total Submissions: 12332 Accepted: 4187

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.

题意:给你一些英文单词,要求将其首尾为相同的字符连接起来,看是否能连乘一个串。

思路:题中给了一个说法,字母出现只会在a~z,也就是说最多26个连接串,这样直接用并查集找是否所有点为一个集合,如果是,再判断欧拉回路,由于是有向图,判断奇数点为两个且均为出度比入度大一或者入度比出度大一,或者所有点入度等于出度

附下代码:

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define maxv 30
int fa[maxv], gree1[maxv], gree2[maxv];
bool vist[maxv];

int find(int x)
{
return x == fa[x] ? x : find(fa[x]);
}

void Union(int x, int y)
{
int root1, root2;
root1 = find(x);
root2 = find(y);
if(root1 != root2)
fa[root2] = root1;
}

int main()
{
int t;
int n;
char str[1010];
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
memset(gree1,0,sizeof(gree1));
memset(gree2,0,sizeof(gree2));
memset(vist,0,sizeof(vist));
for(int i=1; i<=maxv; i++)///初始化父节点一定的要是27个,(题意)
{
fa[i]=i;
}
while(n--)
{
scanf("%s",str);
int a=str[0]-'a';
int b=str[strlen(str)-1]-'a';
//printf("%d %d",a,b);
vist[a]=1;
vist[b]=1;
gree1[a]++;///出度
gree2[b]++;///入度
Union(a,b);
}
int ans=0;
int i;
int x=0,y=0;
int root=0;
int flag1=1,flag=1;
for(int i = 0; i <maxv; ++i)
{
if(vist[i])
{
if(fa[i] == i)
root++;
if(gree1[i] != gree2[i])
{
if(gree1[i] - gree2[i] == 1)
x++;
else if(gree2[i] - gree1[i] == 1)
y++;
else
flag1 = false;
}
}
if(root > 1)
{
flag = false;
break;
}
}
if((flag && y == 0 && x == 0 && flag1) || (flag && y== 1 && x == 1 && flag1))
printf("Ordering is possible.\n");
else
printf("The door cannot be opened.\n");
}
return 0;
}


思路来源:http://blog.csdn.net/niushuai666/article/details/6917777#
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: