poj 3349 Snowflake Snow Snowflakes(hash)

题目链接

Snowflake Snow Snowflakes
Time Limit: 4000MS Memory Limit: 65536K
Total Submissions: 32634 Accepted: 8623

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.
注意:本题有两个坑,第一不能写多组输入,第二不能交c++

题意:n个雪花,每个雪花有6个手臂。告诉你每个手臂的长度(顺时针or逆时针)。如果两个雪花的六个手臂的长度都相同,则两个雪花相同。问是否存在两个相同的雪花?

题解:题目关键在于如何把一个数字构成的环(不知道是顺时针还是逆时针)hash成一个数。

我的做法是,由于不知道是顺时针还是逆时针,所以我们先求出这个环的12种排列方式。用hash来判断这12种之一是否出现过,如果出现过则找到相同的雪花。

由于时限卡得很死,所以我用了读入优化才过。复杂度O(n*(36+36+12))

代码如下:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<set>
#include<vector>
#include<string.h>
#define inff 0x3fffffff
#define nn 7100000
#define mod 7000009
typedef __int64 LL;
typedef unsigned __int64 LLU;
using namespace std;
int n;
int a[7];
LLU ve[13];
bool use[nn];
LLU ha[nn];
void add(LLU x)
{
    int ix=x%mod;
    int i;
//    int fc=0;
    for(i=ix;;i=(i+1)%mod)
    {
        if(!use[i])
        {
            use[i]=true;
            ha[i]=x;
            return ;
        }
        if(ha[i]==x)
            return ;
//        if(++fc>0)
//            break;
    }
}
bool zhao(LLU x)
{
    int ix=x%mod;
    int i;
//    int fc=0;
    for(i=ix;;i=(i+1)%mod)
    {
        if(!use[i])
            return false;
        if(ha[i]==x)
            return true;
//        if(++fc>0)
//            break;
    }
    return false;
}
bool solve()
{
    int lv=0;
    int i,j;
    LLU ix;
    for(i=0;i<6;i++)
    {
        ix=0;
        for(j=0;j<6;j++)
        {
            ix=(ix*10000007+a[(i+j)%6]);
        }
        if(zhao(ix))
            return true;
        ve[lv++]=ix;
    }
    for(i=0;i<6;i++)
    {
        ix=0;
        for(j=0;j<6;j++)
        {
            ix=(ix*10000007+a[((i-j)+6)%6]);
        }
        if(zhao(ix))
            return true;
        ve[lv++]=ix;
    }
    for(i=0;i<12;i++)
    {
        add(ve[i]);
    }
    return false;
}
inline int read()
{
    char ch;
    bool flag=false;
    int re=0;
    while(!(((ch=getchar())>='0'&&(ch<='9'))||(ch=='-')));
    if(ch!='-')
    {
        re*=10;
        re+=ch-'0';
    }
    else
        flag=true;
    while((ch=getchar())>='0'&&(ch<='9'))
    {
        re*=10;
        re+=ch-'0';
    }
    if(flag)
        re=-re;
    return re;
}
int main()
{
    int i;
    scanf("%d",&n);
    {
        memset(use,false,sizeof(use));
        bool ans=false;
        while(n--)
        {
            for(i=0;i<6;i++)
            {
                a[i]=read();
            }
            if(ans)
                continue;
            ans=solve();
        }
        if(ans)
        {
            puts("Twin snowflakes found.");
        }
        else
            puts("No two snowflakes are alike.");
    }
    return 0;
}

但是上一份代码说到底还是卡过的。有没有更快的方法??

我后来的做法是,将一个环直接hash成一个数。具体就是先按前面的方法将一个环hash成了12个数,现在把这12个数进行二次hash,hash成一个数。第一次hash我用的unsigned long long(因为它自动取模)。二次hash有三种方法都可以过:一个是求12个数的异或值,二个事求12个数的平方和,三个是求12个数的立方和。

复杂度O(n*36)。由于hash的数子更少,所以常数比前面种方法小得多。

代码如下:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<set>
#include<vector>
#include<string.h>
#define inff 0x3fffffff
#define nn 1000005
#define mod 1000003
typedef __int64 LL;
typedef unsigned __int64 LLU;
using namespace std;
int n;
int a[7];
bool use[nn];
LLU ha[nn];
void add(LLU x)
{
    int ix=x%mod;
    int i;
    for(i=ix;;i=(i+1)%mod)
    {
        if(!use[i])
        {
            use[i]=true;
            ha[i]=x;
            return ;
        }
    }
}
bool zhao(LLU x)
{
    int ix=x%mod;
    int i;
    for(i=ix;;i=(i+1)%mod)
    {
        if(!use[i])
            return false;
        if(ha[i]==x)
            return true;
    }
    return false;
}
bool solve()
{
    int i,j;
    LLU ix;
    LL fc=0;
    for(i=0;i<6;i++)
    {
        ix=0;
        for(j=0;j<6;j++)
        {
            ix=(ix*10000007+a[(i+j)%6]);
        }
        fc^=ix;
    }
    for(i=0;i<6;i++)
    {
        ix=0;
        for(j=0;j<6;j++)
        {
            ix=(ix*10000007+a[((i-j)+6)%6]);
        }
        fc^=ix;
    }
    if(zhao(fc))
        return true;
    add(fc);
    return false;
}
inline int read()
{
    char ch;
    bool flag=false;
    int re=0;
    while(!(((ch=getchar())>='0'&&(ch<='9'))||(ch=='-')));
    if(ch!='-')
    {
        re*=10;
        re+=ch-'0';
    }
    else
        flag=true;
    while((ch=getchar())>='0'&&(ch<='9'))
    {
        re*=10;
        re+=ch-'0';
    }
    if(flag)
        re=-re;
    return re;
}
int main()
{
    int i;
    scanf("%d",&n);
    {
        memset(use,false,sizeof(use));
        bool ans=false;
        while(n--)
        {
            for(i=0;i<6;i++)
            {
                a[i]=read();
            }
            if(ans)
                continue;
            ans=solve();
        }
        if(ans)
        {
            puts("Twin snowflakes found.");
        }
        else
            puts("No two snowflakes are alike.");
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值