Play on Words POJ

来源:互联网 发布:商陆花软件是什么 编辑:程序博客网 时间:2024/05/22 14:33

题目链接

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
32acmibm3acmmalformmouse2okok
Sample Output
The door cannot be opened.Ordering is possible.The door cannot be opened.
题意:单词拼接,成欧拉通路或欧拉回路,而形成通路或回路,
欧拉回路:图连通,切都为偶度顶点,出度的等于入度。
欧拉通路:欧拉通路:图连通;除2个端点外其余节点入度=出度;1个端点入度比出度大1;一个端点入度比出度小1 或 所有节点入度等于出度。
#include <stdio.h>#include <string.h>int father[30],in[30],out[30],book[30];int find(int x){    if(father[x]==x)        return x;    else return father[x]=find(father[x]);}void merge(int x,int y){    int tx=find(x);    int ty=find(y);    if(tx!=ty)        father[tx]=ty;}int main(){    int t,n;    scanf("%d",&t);    while(t--)    {        memset(in,0,sizeof(in));        memset(out ,0,sizeof(out));        memset(book,0,sizeof(book));        for(int i=1; i<30; i++)            father[i]=i;        char s[2000];        scanf("%d",&n);        for(int i=1; i<=n; i++)        {            scanf("%s",s);            int a=s[0]-'a'+1;            int b=s[strlen(s)-1]-'a'+1;            out[a]++;            in[b]++;            book[a]=book[b]=1;            merge(a,b);        }        int cont=0;        for(int i=1; i<=26; i++)        {            father[i]=find(i);            if(book[i]&&father[i]==i)                cont++;        }        if(cont>1)        {            printf("The door cannot be opened.\n");            continue;        }        cont=0;        int p[1100];        for(int i=1; i<=26; i++)        {            if(book[i]&&out[i]!=in[i])                p[cont++]=i;        }        if(cont==0)        {            printf("Ordering is possible.\n");            continue;        }        if(cont==2)        {            if(in[p[0]]-out[p[0]]==1&&out[p[1]]-in[p[1]]==1||in[p[1]]-out[p[1]]==1&&out[p[0]]-in[p[0]]==1)            {                printf("Ordering is possible.\n");                continue;            }        }        printf("The door cannot be opened.\n");    }    return 0;}


原创粉丝点击