POJ 3349 HASH

题目链接:http://poj.org/problem?id=3349

题意:你可能听说话世界上没有两片相同的雪花,我们定义一个雪花有6个瓣,如果存在有2个雪花相同[雪花是环形的,所以相同可以是旋转过后相同]则输出“Twin snowflakes found.”,否则输出“No two snowflakes are alike.”。

思路:最简单的就是两两判断,但是这样的复杂度为O(n^2),TLE。所以我们要尽量减少判断次数,我们用把拥有6个瓣的雪花HASH成一个数字,只有两个雪花用有相同HASH值时才有"可能"相同,然后HASH难免会有冲突,所以用拉链法解决冲突。

#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<time.h>
#include<set>
using namespace std;
typedef long long int LL;
const int MAXN=100000+5;
const int MOD=999991;
int Num[MAXN][6]; //data storage
struct Node{
    int id,next;
}Snow[MAXN];
int Scnt,Head[MOD];//list table
void Init(){ //initialization
    memset(Head,-1,sizeof(Head));
    Scnt=0;
}
void AddNode(int HashN,int idx){
    Snow[Scnt].id=idx;
    Snow[Scnt].next=Head[HashN];
    Head[HashN]=Scnt++;
}
bool cmp(int idx,int idy){ //compare two Snowflake
    for(int i=0;i<6;i++){ //Sequence order
        bool flag=true;
        for(int st=i,j=0;j<6;j++,st=(st+1==6?0:st+1)){
            if(Num[idx][st]!=Num[idy][j]){
                flag=false;
            }
        }
        if(flag){
            return true;
        }
    }
    for(int i=0;i<6;i++){ //Reverse order
        bool flag=true;
        for(int st=i,j=0;j<6;j++,st=(st-1==(-1)?5:st-1)){
            if(Num[idx][st]!=Num[idy][j]){
                flag=false;
            }
        }
        if(flag){
            return true;
        }
    }
    return false;
}
bool solve(int id){
    int HashNum=0;
    for(int i=0;i<6;i++){ // make hash
        HashNum=(HashNum%MOD+(Num[id][i])%MOD)%MOD;
    }
    for(int i=Head[HashNum];i!=-1;i=Snow[i].next){//get the same hash value
        if(cmp(id,i)){//compare
            return true;
        }
    }
    AddNode(HashNum,id); //insert into hash table
    return false;
}
int main(){
#ifdef kirito
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
#endif
    int start=clock();
    int n,val;
    while(~scanf("%d",&n)){
        bool flag=false; Init();
        for(int i=0;i<n;i++){
            for(int j=0;j<6;j++){
                scanf("%d",&Num[i][j]);
            }
            if(flag){continue;}
            if(solve(i)){
                flag=true;
            }
        }
        if(!flag){
            printf("No two snowflakes are alike.\n");
        }
        else{
            printf("Twin snowflakes found.\n");
        }
    }
#ifdef LOCAL_TIME
    cout << "[Finished in " << clock() - start << " ms]" << endl;
#endif
    return 0;
}
时间: 2024-08-10 15:07:32

POJ 3349 HASH的相关文章

poj 3349 hash的运用

哈希函数思想在查找中是非常重要的一个思想.在数据结构中我们学习的都只是一些简单的函数 比如: 相加取余 相乘取余 相除取余 .... 哈希函数在查找中可以在O(1)时间中查找到数据的位置. 哈希函数的关键在于函数的选取 , 然而不管选择怎么样的函数 , 一般都会存在冲突 , 但是如果函数选取得得当,那么冲突就会减小. poj 3349是一题简单的hash题 我们选取的函数是: 相加取余数 sort(b , b+6 ); x = 0; for(j = 0; j < 6; j++) x = (x*1

hash应用以及vector的使用简介:POJ 3349 Snowflake Snow Snowflakes

今天学的hash.说实话还没怎么搞懂,明天有时间把知识点总结写了,今天就小小的写个结题报告吧! 题意: 在n (n<100000)个雪花中判断是否存在两片完全相同的雪花,每片雪花有6个角,每个角的长度限制为1000000 两片雪花相等的条件: 雪花6个角的长度按顺序相等(这个顺序即可以是顺时针的也可以是逆时针的) 解题思路: hash:连加求余法 求key 值,链地址法解决冲突,连加求余法 求key 值挺简单,关于链地址法解决冲突可以通过c++中,vector容器可以较为方便的实现. 下面先介绍

poj 3349 数组的hash(最常用、最普通的哈希表建立)

http://poj.org/problem?id=3349 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

POJ 3349 Snowflake Snow Snowflakes (哈希表)

题意:每片雪花有六瓣,给出n片雪花,六瓣花瓣的长度按顺时针或逆时针给出,判断其中有没有相同的雪花(六瓣花瓣的长度相同) 思路:如果直接遍历会超时,我试过.这里要用哈希表,哈希表的关键码key用六瓣花瓣的长度的和取余一个数得到,表中为雪花的存储位置address(即在snowflakes数组中的位置) 代码: #include<iostream> #include<vector> using namespace std; const int maxn=100000+100;//雪花最

POJ 3349 Snowflake Snow Snowflakes(哈希表)(转)

题意:判断有没有两朵相同的雪花.每朵雪花有六瓣,比较花瓣长度的方法看是否是一样的,如果对应的arms有相同的长度说明是一样的.给出n朵,只要有两朵是一样的就输出有Twin snowflakes found.,如果任何两个都是不一样的输出No two snowflakes are alike.n=100,000. 思路:最 简单的就是枚举每两片雪花,判断他们是否相同.时间复杂度为O(n*n),显然效果不理想.有没有更好的算法呢?hash:每读进一片雪花,将雪花 hash,判断hash表里是否有相同

poj 3349:Snowflake Snow Snowflakes(哈希查找,求和取余法+拉链法)

Snowflake Snow Snowflakes Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 30529   Accepted: 8033 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. Y

poj 3156 hash+记忆化搜索 期望dp

#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> using namespace std; int n,m; #define N 32 #define mod 10007LL #define mod2 10000007 #define inf 0x3fffffff typedef long long ll; typedef double dd; int f[N]

POJ 3349:Snowflake Snow Snowflakes(数的Hash)

http://poj.org/problem?id=3349 Snowflake Snow Snowflakes Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 37609   Accepted: 9878 Description You may have heard that no two snowflakes are alike. Your task is to write a program to determine

POJ 3349 - Snowflake Snow Snowflakes - [hash]

题目链接:http://poj.org/problem?id=3349 Time Limit: 4000MS Memory Limit: 65536K 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