POJ3349 Snowflake Snow Snowflakes 【哈希表】

题目

很简单,给一堆6元组,可以从任意位置开始往任意方向读,问有没有两个相同的6元组

题解

hash表入门题

先把一个六元组的积 + 和取模作为hash值,然后查表即可

期望\(O(n)\)

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#define LL long long int
#define Redge(u) for (int k = h[u]; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
using namespace std;
const int maxn = 100010,maxm = 100005,INF = 1000000000,P = 100007;
inline int read(){
    int out = 0,flag = 1; char c = getchar();
    while (c < 48 || c > 57){if (c == ‘-‘) flag = -1; c = getchar();}
    while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
    return out * flag;
}
int h[maxn],nxt[maxn],n,tot;
LL A[6],H[maxn][6];
bool cmp(LL A[],LL B[]){
    for (int pos = 0; pos < 6; pos++){
        bool flag = true;
        for (int i = 0; i < 6; i++)
            if (A[i] != B[(pos + i) % 6]){
                flag = false; break;
            }
        if (flag) return true;
        flag = true;
        for (int i = 0; i < 6; i++)
            if (A[i] != B[(pos - i + 6) % 6]){
                flag = false; break;
            }
        if (flag) return true;
    }
    return false;
}
bool ins(){
    LL s = 0,m = 1,v;
    for (int i = 0; i < 6; i++)
        s = (s + A[i]) % P,m = m * A[i] % P;
    v = (s + m) % P;
    for (int k = h[v]; k != -1; k = nxt[k])
        if (cmp(H[k],A)) return true;
    tot++;
    for (int i = 0; i < 6; i++) H[tot][i] = A[i];
    nxt[tot] = h[v]; h[v] = tot;
    return false;
}
int main(){
    memset(h,-1,sizeof(h));
    n = read();
    for (int i = 1; i <= n; i++){
        for (int j = 0; j < 6; j++)
            A[j] = read();
        if (ins()) {puts("Twin snowflakes found."); return 0;}
    }
    puts("No two snowflakes are alike.");
    return 0;
}

原文地址:https://www.cnblogs.com/Mychael/p/8508431.html

时间: 2024-10-22 23:12:17

POJ3349 Snowflake Snow Snowflakes 【哈希表】的相关文章

[poj3349]Snowflake Snow Snowflakes(hash)

Snowflake Snow Snowflakes Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 37615 Accepted: 9882 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

POJ3349 Snowflake Snow Snowflakes (hash

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

POJ3349: Snowflake Snow Snowflakes(hash 表)

考察hash表: 每一个雪花都有各自的6个arm值,如果两个雪花从相同或者不同位置开始顺时针数或者逆时针数可以匹配上,那么这两个雪花就是相等的. 我们采用hash的方法,这样每次查询用时为O(1),总用时为O(n). hash的本质是把值映射到地址或者下标,如果不同的key值对应到相同的indice上的话,就需要进行chaining处理,吧indice指向一个链表,链表的每一个节点存储共享同一indice的不同key值. 因此问题的核心变成:吧相等的雪花映射到相同的indice上.这里雪花是一个

POJ3349 Snowflake Snow Snowflakes

Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 39075   Accepted: 10232 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 inf

Hash poj3349 Snowflake Snow Snowflakes

题意:判断是否有两片一样的雪花. Hash第一题,基本是抄的. ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75

POJ 3349 Snowflake Snow Snowflakes 哈希(链式解决冲突)

题意:n个数列 每个数列6个元素a[i],a[i]<=1e7,两个数列只要,经过若干次循环移动能相等则定义为相似.n<=1e5,问n个数列中 是否存在两个数列相似? 每个数列只有6个数,则相似的最多12种,对每个数列算出其hash值 相同hash值插入同一个链表中.查看新输入的数列插入时是否产生冲突即可 O(n*12) #include <iostream> #include <cstdio> #include <cstring> #include <

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