Play on Words——字母首尾相连

Play on Words
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 10138 Accepted: 3468

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.

题意:一扇门装了密码锁,给出一些单词,将这些单词首尾相连,连接的部分两个字母相同,即一个单词的最后一个字母和一个单词的第一个字母相同才能连在一起,这样,如果把这些单词都能够连接起来,那么就可以把门打开,问能否把门打开。

解析:首先,我们要知道这些单词能否首尾相连,如果不能首尾相连,那么自然也就不能打开门。注意到,题目中给出的单词只有第一个字母和最后一个字母有用,是这两个字母之间的对接,那么剩下的字母就像边一样连接了两端的字母这两个顶点,这样我们就成功的把这个问题转化成了图。能不能把门打开,就是看能不能将这些单词首尾连成一条线,实际上就是求存不存在有向欧拉连通路径。

有向欧拉连通路径:设D是有向图,D的基图连通,并且D的每条边经过且仅经过一次的有向路径。

那么我们就要判断基图连不连通,判断每条边经过且仅经过一次。

判断每条边经过且仅经过一次:看每个顶点的入度和出度,具体根据有向欧拉通路的定义来判断。

判断基图连不连通,用并查集看每个顶点是不是都连通在一棵树上,具体看并查集的相关知识点

#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

using namespace std;

int od[26],id[26];
int parent[26],bused[26];
int n,t,u,v;
char word[1006];

int Find(int x)
{
    int s = x;
    while(parent[s] >= 0)
        s = parent[s];
        //并查集的路径压缩
    while(s != x)
    {
        int tmp = parent[x];
        parent[x] = s;
        x = tmp;
    }
    return s;
}

void Union(int x,int y)
{
    int fx = Find(x);
    int fy = Find(y);
    if(fx != fy)
    {
        //并查集的加权法则,尽量使树高降低
        int sum = parent[fx] + parent[fy];
        if(parent[fx] < parent[fy])
        {
            parent[fy] = fx;
            parent[fx] = sum;
        }
        else
        {
            parent[fx] = fy;
            parent[fy] = sum;
        }
    }
}

int main()
{
    int i,j,k,l;
    scanf("%d",&t);
    while(t--)
    {
        memset(od,0,sizeof(od));
        memset(id,0,sizeof(id));
        memset(parent,-1,sizeof(parent));
        memset(bused,0,sizeof(bused));
        scanf("%d",&n);
        bool falg = true;
        for(i = 0; i < n; i++)
        {
            scanf("%s",word);
            l = strlen(word);
            u = word[0] - 'a';
            v = word[l-1] - 'a';
            od[u]++;
            id[v]++;
            //说明该顶点出现过
            bused[u] = bused[v] = 1;
            //在输入边的时候进行并查集建树
            Union(u,v);
        }
        int first = -1,num;
        //看是否连通
        for(i = 0; i < 26; i++)
        {
            if(bused[i] == 0)
                continue;
            if(first == -1)
            {
                first = i;
                num = Find(first);
            }
            if(num != Find(i))
            {
                falg = false;
                break;
            }
        }
        if(falg == false)
        {
            printf("The door cannot be opened.\n");
            continue;
        }
        //在连通的基础上判断每条边是不是都经过一次并且仅经过一次
        int a = 0,b = 0;
        for(i = 0; i < 26; i++)
        {
            if(bused[i] == 0)
                continue;
            if(od[i] - id[i] == 1)
            {
                a++;
                if(a > 1)
                {
                    falg = false;
                    break;
                }
            }
            if(id[i] - od[i] == 1)
            {
                b++;
                if(b > 1)
                {
                    falg = false;
                    break;
                }
            }
            if(id[i]-od[i] > 1 || od[i]-id[i]>1)
            {
                falg = false;
                break;
            }
        }
        if(a != b)
            falg = false;
        if(falg == false)
        {
            printf("The door cannot be opened.\n");
        }
        else
            printf("Ordering is possible.\n");
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值