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;
}

 

转载于:https://www.cnblogs.com/chenjianxiang/p/3511353.html

乐播投屏是一款简单好用、功能强大的专业投屏软件,支持手机投屏电视、手机投电脑、电脑投电视等多种投屏方式。 多端兼容与跨网投屏:支持手机、平板、电脑等多种设备之间的自由组合投屏,且无需连接 WiFi,通过跨屏技术打破网络限制,扫一扫即可投屏。 广泛的应用支持:支持 10000+APP 投屏,包括综合视频、网盘与浏览器、美韩剧、斗鱼、虎牙等直播平台,还能将央视、湖南卫视等各大卫视的直播内容一键投屏。 高清流畅投屏体验:腾讯独家智能音画调校技术,支持 4K 高清画质、240Hz 超高帧率,低延迟不卡顿,能为用户提供更高清、流畅的视觉享受。 会议办公功能强大:拥有全球唯一的 “超级投屏空间”,扫码即投,无需安装。支持多人共享投屏、远程协作批注,PPT、Excel、视频等文件都能流畅展示,还具备企业级安全加密,保障会议资料不泄露。 多人互动功能:支持多人投屏,邀请好友加入投屏互动,远程也可加入。同时具备一屏多显、语音互动功能,支持多人连麦,实时语音交流。 文件支持全面:支持 PPT、PDF、Word、Excel 等办公文件,以及视频、图片等多种类型文件的投屏,还支持网盘直投,无需下载和转格式。 特色功能丰富:投屏时可同步录制投屏画面,部分版本还支持通过触控屏或电视端外接鼠标反控电脑,以及在投屏过程中用画笔实时标注等功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值