1. 程式人生 > >POJ 3349 - Snowflake Snow Snowflakes - [hash]

POJ 3349 - Snowflake Snow Snowflakes - [hash]

!= can urn info 現在 題意 hat mod left

題目鏈接: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 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.

題意:

給出 $n$ 個六元組代表 $n$ 個雪花,第 $i$ 個雪花的 $a_i = \left( {a_{i,1} ,a_{i,2} ,a_{i,3} ,a_{i,4} ,a_{i,5} ,a_{i,6} } \right)$,代表雪花的六個角的長度,

對於某兩個雪花,若滿足從它們各自的某一個角出發,順時針或者逆時針旋轉記錄長度,能得到相同的兩個六元組,則認為兩個雪花形狀相同,

現在求在這 $n$ 個雪花中,是否存在兩個雪花是形狀相同的。

題解:

構造哈希函數 $H\left( {a_i } \right) = \left( {\sum\limits_{j = 1}^6 {a_{i,j} } + \prod\limits_{j = 1}^6 {a_{i,j} } } \right)\bmod P$,其中 $P$ 是一個和 $N$ 接近的質數,

顯然,對於兩個形狀相同的雪花,其哈希函數函數值應當是相同的,建立hash表(鄰接表式),若某個鏈表表頭下領著兩個及以上節點,就說明有形狀形同的雪花。

時間復雜度:暴力枚舉 $n$ 個雪花,每一次都進入hash表中的某個鏈表查詢,鏈表規模 $O\left( {\frac{n}{P}} \right)$(隨機數據),總時間復雜度 $O\left( {\frac{{n^2 }}{P}} \right)$,當 $P$ 是一個和 $N$ 接近的質數時,時間復雜度接近 $O\left( n \right)$。

AC代碼:

#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;

const int maxn=100000+10;
const int P=99991;

int n;

struct SF{
    int a[6];
}table[maxn];
int tot,head[maxn],next[maxn];

int H(const SF &sf)
{
    int sum=0,mul=1;
    for(int k=0;k<6;k++)
    {
        sum=(sum+sf.a[k])%P;
        mul=((ll)mul*sf.a[k])%P;
    }
    return (sum+mul)%P;
}

bool isSame(const SF &A,const SF &B)
{
    bool ok;
    for(int i=0;i<6;i++)
    {
        for(int j=0;j<6;j++)
        {
            ok=1;
            for(int k=0;k<6;k++) if(A.a[(i+k)%6]!=B.a[(j+k)%6]) ok=0;
            if(ok) return 1;
            ok=1;
            for(int k=0;k<6;k++) if(A.a[(i+k)%6]!=B.a[(j-k+6)%6]) ok=0;
            if(ok) return 1;
        }
    }
    return 0;
}

bool Insert(const SF &sf)
{
    int has=H(sf);
    for(int i=head[has];i;i=next[i]) if(isSame(table[i],sf)) return 1;

    table[++tot]=sf;
    next[tot]=head[has];
    head[has]=tot;
    return 0;
}

int main()
{
    tot=0;
    memset(head,0,sizeof(head));

    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        SF sf;
        for(int k=0;k<6;k++) scanf("%d",&sf.a[k]);
        if(Insert(sf))
        {
            printf("Twin snowflakes found.\n");
            return 0;
        }
    }
    printf("No two snowflakes are alike.\n");
}

用vector寫鄰接表被卡了……老老實實改用數組模擬……

POJ 3349 - Snowflake Snow Snowflakes - [hash]