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

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.
/**
poj 3349  数字hash
题目大意:每个雪花都有六个分支,用六个整数代表,这六个整数是从任意一个分支开始,朝顺时针或逆时针方向遍历得到的。输入多个雪花,判断是否有形状一致的雪花存在。
解题思路:数字哈希,要注意的是每种雪花可以由多种数字组合表示。
比如输入的是1 2 3 4 5 6,
则2 3 4 5 6 1,3 4  5 6 1 2,……,6 5 4 3 2 1,5 4 3 2 1 6等都是相同形状的。
因此可以在读入一个雪花的时候把这些情况全部放入哈希表中,如果某次插入的时候发生冲突,则说明存在重复的雪花,并且后面的不需要再处理。
*/
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long LL;
const int maxn=1200010;
const int mod=1200007;

int head[maxn],ip;

void init()
{
    memset(head,-1,sizeof(head));
    ip=0;
}
struct note
{
    int num[6];
    int next;
} edge[1200010];

int get_hash(int *num)
{
    int h=0;
    for(int i=0; i<6; i++)
        h+=num[i];///哈希函数,也可以用其他的构造方式
    return h%mod;
}

void insert_hash(int *num,int h)
{
    for(int i=0; i<6; i++)
        edge[ip].num[i]=num[i];
    edge[ip].next=head[h];
    head[h]=ip;
    ip++;

}

bool compare(int *a,int *b)
{
    for(int i=0; i<6; i++)
    {
        if(a[i]!=b[i])
            return false;
    }
    return true;
}

bool search_hash(int *num)
{
    int h=get_hash(num);
    for(int i=head[h]; i!=-1; i=edge[i].next)
    {
        if(compare(num,edge[i].num))
            return true;
    }
    insert_hash(num,h);
    return false;
}

int main()
{
    int n,num[2][15];
    scanf("%d",&n);
    init();
    int flag=0;
    while(n--)
    {
        for(int i=0; i<6; i++)
        {
            scanf("%d",&num[0][i]);
            num[0][i+6]=num[0][i];
        }
        if(flag) continue;
        for(int i=0; i<6; i++)
        {
            num[1][i+6]=num[1][i]=num[0][5-i];
        }
        for(int i=0; i<6; i++)
        {
            if(search_hash(num[0]+i)||search_hash(num[1]+i))
            {
                flag=1;
                break;
            }
        }
    }
    if(flag)printf("Twin snowflakes found.\n");
    else printf("No two snowflakes are alike.\n");
    return 0;
}


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。