poj 3349(hash判重)

本文介绍了一种用于比对雪flake是否相同的算法实现。通过旋转和翻转雪flake的六边形结构,寻找字典序最小的状态进行哈希存储,以此来判断是否有完全相同的两个雪flake。该算法在比赛中获得了成功应用。

挺有意思的题。

思路不难, 用hash判重, 但是要坑人的是一开始怎么将每串序列按照同一规律排列。 

比如

2 1 2 1 1 2  

1 1 2 2 1 2

怎么转化为 1 1 2 2 1 2 ,也就是转化为字典序最小, 最后没有办法只有枚举, 3000+ms 险过.

 

Snowflake Snow Snowflakes
Time Limit: 4000MS Memory Limit: 65536K
Total Submissions: 26254 Accepted: 6900

Description

You may have heard that no two snowflakes are alike. Your task is to write a program to determine whether this is really true. Your program will read information about a collection of snowflakes, and search for a pair that may be identical. Each snowflake has six arms. For each snowflake, your program will be provided with a measurement of the length of each of the six arms. Any pair of snowflakes which have the same lengths of corresponding arms should be flagged by your program as possibly identical.

Input

The first line of input will contain a single integer n, 0 < n ≤ 100000, the number of snowflakes to follow. This will be followed by n lines, each describing a snowflake. Each snowflake will be described by a line containing six integers (each integer is at least 0 and less than 10000000), the lengths of the arms of the snow ake. The lengths of the arms will be given in order around the snowflake (either clockwise or counterclockwise), but they may begin with any of the six arms. For example, the same snowflake could be described as 1 2 3 4 5 6 or 4 3 2 1 6 5.

Output

If all of the snowflakes are distinct, your program should print the message:
No two snowflakes are alike.
If there is a pair of possibly identical snow akes, your program should print the message:
Twin snowflakes found.

Sample Input

2
1 2 3 4 5 6
4 3 2 1 6 5

Sample Output

Twin snowflakes found.

Source

 

#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
#define N 1010000
#define MOD 1000007
#define INF 10000001
typedef __int64 LL;

struct node
{
    int next;
    int g[6];
}edge[N];

int cnt,pre[N];
int flag1;


void hash(LL key,int k[6])
{
    for(int p=pre[key];p!=-1;p=edge[p].next)
    {
        int ff=0;
        for(int i=0;i<6;i++)
            if(k[i]!=edge[p].g[i])
            {
                ff=1;
                break;
            }
        if(ff==0)
        {
            flag1=1;
            return ;
        }
    }
    for(int i=0;i<6;i++)
        edge[cnt].g[i]=k[i];
    edge[cnt].next=pre[key];
    pre[key]=cnt++;
}

int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        int k[6],tk[6],mk[6];
        cnt=0;
        memset(pre,-1,sizeof(pre));
        flag1=0;
        for(int i=0;i<n;i++)
        {
            for(int i=0;i<6;i++)
            mk[i]=INF;
            for(int j=0;j<6;j++)
            {
                scanf("%d",&k[j]);
            }
            if(flag1==1) continue;
            /*int flag=1;
            int t1=s,t2=s;
            for(int j=0;j<5;j++)
            {
                t1++;
                if(t1>=6) t1-=6;
                t2--;
                if(t2<0) t2+=6;
                if(k[t1]>k[t2])
                {
                    flag=-1; // 说明是逆向的
                    break;
                }
                if(k[t1]<k[t2])
                {
                    flag=1;
                    break;
                }
            }
            for(int j=0;j<6;j++)
            {
                tk[j]=k[s];
                s += flag;
                if(s>=6) s-=6;
                if(s<0) s+=6;
            }*/
            for(int j=0;j<6;j++)
            {
                for(int i1=0;i1<6;i1++)
                    tk[i1]=k[(i1+j)%6];
                int f=0;
                for(int i1=0;i1<6;i1++)
                {
                    if(mk[i1]<tk[i1])
                    {
                        break;
                    }
                    if(mk[i1]>tk[i1])
                    {
                        f=1;
                        break;
                    }
                }
                if(f==1)
                {
                    for(int i1=0;i1<6;i1++)
                        mk[i1]=tk[i1];
                }
            }
            for(int j=0;j<6;j++)
            {
                for(int i1=0;i1<6;i1++)
                    tk[i1]=k[(j-i1+6)%6];
                int f=0;
                for(int i1=0;i1<6;i1++)
                {
                    if(mk[i1]<tk[i1])
                    {
                        break;
                    }
                    if(mk[i1]>tk[i1])
                    {
                        f=1;
                        break;
                    }
                }
                if(f==1)
                {
                    for(int i1=0;i1<6;i1++)
                        mk[i1]=tk[i1];
                }
            }
            LL tmp=0;
            for(int j=0;j<6;j++)
            {
                tmp=(tmp*100000007+mk[j])%MOD;
            }
            hash(tmp,mk);
        }
        if(flag1==0) printf("No two snowflakes are alike.\n");
        else printf("Twin snowflakes found.\n");
    }
    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、付费专栏及课程。

余额充值