poj 1386 Play on Words

本文介绍了一个有趣的单词拼接谜题,通过编程解决考古学家遇到的问题:如何将一系列单词重新排列,使得每个单词的第一个字母与前一个单词的最后一个字母相同。文章详细解释了解决方案的思路和技术实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Play on Words
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 8936 Accepted: 3109

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

题意:给出一系列单词,看能不能通过重组单词的顺序使得每个单词第一个字母和前一个单词的最后一个字母相同

思路:本题主要有两个关键,一是判断欧拉回路,二是判断基图是否连通,判断图连通的时候可以用并查集来平判断。将每个单词的首尾字母作为图的结点,每个单词为图中的一条边。

#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int in[26] , out[26];///每个字母的出度和入度。
#define maxn 100001
int T , N , used[26] , parent[26];///used数组表示改字母是否作为首尾字母
char word[1001];
struct node
{
    int u , v;
}edges[maxn];
void UFset()
{
    for(int i=0; i<26; ++i)
        parent[i] = -1;
}
int Find(int x)///赤裸裸的并查集,无话可说,随便怎么写=。=
{
    int s;
    for(s=x; parent[s] >= 0; s=parent[s]);
    while(s != x)
    {
        int tmp = parent[x];
        parent[x] = s;
        x = tmp;
    }
    return s;
}
void Union(int r1 , int r2)
{
    int root1 = Find(r1) , root2 = Find(r2);
    int tmp = parent[root1] + parent[root2];
    if(parent[root1] > parent[root2])
    {
        parent[root1] = root2;
        parent[root2] = tmp;
    }
    else
    {
        parent[root2] = root1;
        parent[root1] = tmp;
    }
}
bool bconnect()///判断图是否连通
{
    int u , v , i;
    UFset();
    for(int i=0; i<N; ++i)///对每条边(u,v),如果u和v不属于同一个连通分量,将其合并。
    {
        u = edges[i].u;
        v = edges[i].v;
        if(u != v && Find(u) != Find(v))
            Union(u , v);
    }
    int root = -1;///第一个used[i]不为0的顶点。
    for(i=0; i<26; ++i)
    {
        if(used[i] == 0) continue;
        if(root == -1) root = i;
        else if(Find(i) != Find(root))  break;///只有一个树根
    }
    if(i < 26) return false;///不连通。
    else return true;
}
int main()
{
    int u , v;
    scanf("%d", &T);
    while(T--)
    {
        memset(in, 0, sizeof(in));
        memset(out, 0, sizeof(out));
        memset(used, 0, sizeof(used));
        scanf("%d", &N);
        for(int i=0; i<N; ++i)
        {
            scanf("%s", word);
            u = word[0] - 'a'; v = word[strlen(word) - 1] - 'a';
            out[u]++;
            in[v]++;
            used[u] = used[v] = 1;
            edges[i].u = u;///建图
            edges[i].v = v;
        }
        bool flag = true;
        int start = 0;
        ///有向图中如果有欧拉通路,最多只能有两个点出度与入度差为1或-1,并且如果有这种点,这两个点一定分别是起点和终点,这里start和end就是表示这两个点。
        int End = 0;
        for(int i=0; i<26; ++i)
        {
            if(used[i] == 0) continue;
            if(out[i] - in[i] >= 2 || in[i] - out[i] >= 2)
            {
                flag = false;
                break;
            }///出度与入度的差的绝对值如果大于2,一定没有欧拉通路。
            if(out[i] - in[i] == 1)
            {
                start++;
                if(start > 1)
                {
                    flag = false;
                    break;
                }
            }
            if(in[i] - out[i] == 1)
            {

                End++;
                if(End > 1)
                {
                    flag = false;
                    break;
                }
            }
        }
        if(start != End) flag = false;///判断起点数和终点数,因为可能出现只有一个点出度与入度差的绝对值为1的情况,这种情况是没有欧拉通路的。
        if(!bconnect()) flag = false;///判断图是否连通。
        if(flag) printf("Ordering is possible.\n");
        else printf("The door cannot be opened.\n");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值