• TOJ 1836 Play on Words


    描述

    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.

    输入

    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.

    输出

    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.".

    样例输入

    3
    2
    acm
    ibm
    3
    acm
    malform
    mouse
    2
    ok
    ok
    

    样例输出

    The door cannot be opened.
    Ordering is possible.
    The door cannot be opened.
    

    解题:
    把这题转化成欧拉回路问题就可以了。。。
    首先把每一行的第一个字母当做有向图的起始位置,结束的字母当做有向图的结束位置
     
    比如: 
    acm malform mouse
    那么就是 a->m  m->m m->e   =>   a->m->e
    再来看看这样算不合法的
    o->k
    o->k
    就不合法了,o的出度为2,入度为0,k的入度为2,出度为0
    a->m
    i->m
    也不合法,m的入度为2,出度为0

    该问题可以转换为欧拉回路问题。
    1,保证改图是连通的。
    2,如果它是环形,那么它的每一个结点的出度=入度。
    3,如果它是链状,那么只有它的起始结点(出度-入度=1)和结束结点(入度-出度=1)
     
     
    #include <stdio.h>
    #include <string.h>
    int a[30];
    int flag[30];
    int abs(int a){
        if(a<0)return -a;
        else return a;
    }
    int find(int x){
        int temp=a[x];
        while(temp!=a[temp]){
            temp=a[temp];
        }
        int v;
        while(x!=a[x]){
            v=a[x];
            a[x]=temp;
            x=v;
        }
        return temp;
    }
    void u(int x,int y){
        x=find(x);
        y=find(y);
        if(x!=y){
            a[x]=y;
        }
    }
    int main(int argc, char *argv[])
    {
        int t;
        int begin,end;
        char ch[1001];
        scanf("%d",&t);
        while(t--){       
            int n;
            int in[30],out[30];
            scanf("%d",&n);       
            memset(flag,0,sizeof(flag)); 
            memset(in,0,sizeof(in)); 
            memset(out,0,sizeof(out));
            for(int i=0; i<=25; i++)a[i]=i;
            while(n--){                   
                scanf("%s",ch);
                int len=strlen(ch);
                begin=ch[0]-'a';
                end=ch[len-1]-'a';
                flag[begin]=1;
                flag[end]=1;
                in[end]++;
                out[begin]++;
                u(begin,end);       
            }
            int s=0;
            for(int i=0; i<=25; i++){
                if(flag[i]==1 && a[i]==i)s+=1;
            }
            if(s==1){
                int f_in=0,f_out=0;
                int k;
                for( k=0; k<=25; k++){
                    if(flag[k]==1){
                        if(in[k]==out[k]){
                            continue;
                        }else if(abs(in[k]-out[k])==1){
                            if(in[k]-out[k]<0)f_out++;
                            if(in[k]-out[k]>0)f_in++;
                            if(f_out>1 || f_in>1)break;
                        }else if(abs(in[k]-out[k])>1){
                            break;
                        }
                    }
                }
                if(k==26){
                    puts("Ordering is possible.");
                }else{
                    puts("The door cannot be opened.");
                }
            }else{
                puts("The door cannot be opened.");
            }
        }
        return 0;
    }
    
  • 相关阅读:
    H5新增属性02
    h5新增属性
    javascript基础
    多列布局和弹性盒模型详解
    边框图片+盒子倒影
    CSS新增边框属性
    css3选择器
    css3基础
    h5新增-2
    h5新增
  • 原文地址:https://www.cnblogs.com/chenjianxiang/p/3511353.html
Copyright © 2020-2023  润新知