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. 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.

代码如下:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 1e5+5;
int n,tot,P=99991;
int snow[maxn][6];
int head[maxn];
int nex[maxn];
int H(int *a){
    int sum=0;
    int mul=1;
    for(int i=0;i<6;i++){
        sum=(sum+a[i])%P;
        mul=(long long)mul*a[i]%P;
    }
    return (sum+mul)%P;
}
bool equal(int *a,int *b){
    for(int i=0;i<6;i++){
        for(int j=0;j<6;j++){
            bool eq=1;
            for(int k=0;k<6;k++){
                if(a[(i+k)%6]!=b[(j+k)%6]) eq=0;
            }
            if(eq) return 1;
            eq=1;
            for(int k=0;k<6;k++){
                if(a[(i+k)%6]!=b[(j-k+6)%6]) eq=0;
            }
            if(eq) return 1;
        }
    }
    return 0;
}

bool insert(int *a){
    int val=H(a);
    for(int i=head[val];i;i=nex[i]){
        if(equal(snow[i],a)) return 1;
    }
    ++tot;
    memcpy(snow[tot],a,6*sizeof(int));
    nex[tot]=head[val];
    head[val]=tot;
    return 0;
}
int main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        int a[10];
        for(int j=0;j<6;j++) scanf("%d",&a[j]);
            if(insert(a)){
                puts("Twin snowflakes found.");
                return 0;
            }
    }
    puts("No two snowflakes are alike.");
}

原文地址:https://www.cnblogs.com/buerdepepeqi/p/9452951.html

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

POJ3349 Snowflake Snow Snowflakes (hash的相关文章

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

[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

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

[ACM] POJ 3349 Snowflake Snow Snowflakes(哈希查找,链式解决冲突)

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

poj3349--Snowflake Snow Snowflakes(hash)

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

POJ3349: Snowflake Snow Snowflakes(hash 表)

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

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