Play on Words POJ - 1386

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.

题解:这道题的意思是要 输入一些英文单词,根据该单词的首尾字母,判断所有单词能不能连成一串,类似于成语接龙的意思。同样如果有多个重复的单词时,也必须满足这样的条件才能通过,否则都是不可能的情况。这道题要用到并查集以及欧拉图,要确保给出的单词能够连通。

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
char mp[1010];
int f[30],book[30],a[30],b[30];
int getf(int u)
{
    if(f[u]!=u)
        f[u]=getf(f[u]);
    return f[u];
}
void mergee(int u,int v)
{
    int t1=getf(u),t2=getf(v);
    if(t1!=t2)
        f[t2]=t1;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int i,j,n,l;
        memset(book,0,sizeof(book));
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        for(i=0; i<30; i++)
            f[i]=i;
        scanf("%d",&n);
        getchar();
        for(i=0; i<n; i++)
        {
            int st,ed;
            scanf("%s",mp);
            l=strlen(mp);
            st=mp[0]-'a'+1;
            ed=mp[l-1]-'a'+1;
            book[st]=1;
            book[ed]=1;
            a[st]++;
            b[ed]++;//头尾出现次数
            mergee(st,ed);
        }
        int k1=0,k2=0,f1=0,f2=0,ans=0;
        for(i=0; i<30; i++)
        {
            if(book[i])
            {
                if(f[i]==i)
                    ans++;//说明i没联通或者它是首
                if(a[i]!=b[i])
                {
                    if(a[i]-b[i]==1)
                        f1++;//作头或者作尾都不能多,最多一次
                    else if(b[i]-a[i]==1)
                        f2++;
                    else//太多直接不行
                        k1=1;
                }
            }
            if(ans>1)
            {
                k2=1;
                break;
            }
        }
        if(!k1&&!k2&&f1<2&&f2<2)
            printf("Ordering is possible.\n");
        else
            printf("The door cannot be opened.\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/GJLfly/article/details/81668712