poj 1386 Play on Words

本文探讨了一种独特的磁盘排列谜题,该谜题是考古学家打开隐藏房间的关键。通过理解每块磁盘上的单词连接规则,我们能够运用欧拉路径的概念来确定是否有可能将所有磁盘按顺序排列。文章详细介绍了输入格式、解题策略以及输出结果的判断逻辑。通过并查集维护连通性,确保所有磁盘按照规则排列的可能性得到准确评估。

Play on Words
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 9676 Accepted: 3341

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.


建图规则:对每个单词首字母到尾字母连一条有向边,如果单词x1(a1->b1)的尾字母等于单次y1(b1->c1),可以发现a1经b1到达c1,因此若干的单词首尾相连,从首字符走到尾字符,中间经过若干字符,每个字符都是k次进,k次出,立马想到欧拉通路,注意一下特殊情况:最后一个单词的尾字符和第一个单词的首字符相同,那么就是欧拉回路了。

而有向图欧拉通路的充要条件:基图连通,并且除了两个顶点,一个入度-出度=1,另一个出度-入度=1,其余点入度=出度。

有向图欧拉回路的充要条件:基图连通,所有顶点的入度=出度。

然后用并查集维护一下连通性,开个vis数组表示字符是否在图中出现即可。


代码:

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;

int ideg[30],odeg[30],fa[30],vis[30];
char s[1010];
void init(){
    for(int i=0;i<26;i++)
        fa[i]=i;
}
int findset(int x){
    return fa[x]==x?x:(fa[x]=findset(fa[x]));
}
void unionset(int a,int b){
    a=findset(a),b=findset(b);
    fa[a]=b;
}
int main()
{
    int t,n;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        memset(ideg,0,sizeof ideg);
        memset(odeg,0,sizeof odeg);
        memset(vis,0,sizeof vis);
        init();
        for(int i=0;i<n;i++){
            scanf("%s",s);
            int len=strlen(s);
            int st=s[0]-'a',e=s[len-1]-'a';
            vis[st]=vis[e]=1;
            odeg[st]++,ideg[e]++;
            unionset(st,e);
        }
        bool flag=true;
        int t;
        for(int i=0;i<26;i++)
            if(vis[i]) {t=findset(i);break;}
        for(int i=1;i<26;i++)
            if(vis[i]&&findset(i)!=t) {flag=false;break;}
        int in=0,out=0;
        for(int i=0;i<26;i++){
            t=ideg[i]-odeg[i];
            if(t==1) in++;
            else if(t==-1) out++;
            if(t>1||t<-1||in>1||out>1) {flag=false;break;}
        }
        if(flag) puts("Ordering is possible.");
        else puts("The door cannot be opened.");
    }
	return 0;
}


内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值