POJ 3349 - Snowflake Snow Snowflakes - [hash]
Posted dilthey
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 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} } ight)$,代表雪花的六个角的长度,
对于某两个雪花,若满足从它们各自的某一个角出发,顺时针或者逆时针旋转记录长度,能得到相同的两个六元组,则认为两个雪花形状相同,
现在求在这 $n$ 个雪花中,是否存在两个雪花是形状相同的。
题解:
构造哈希函数 $Hleft( {a_i } ight) = left( {sumlimits_{j = 1}^6 {a_{i,j} } + prodlimits_{j = 1}^6 {a_{i,j} } } ight)mod P$,其中 $P$ 是一个和 $N$ 接近的质数,
显然,对于两个形状相同的雪花,其哈希函数函数值应当是相同的,建立hash表(邻接表式),若某个链表表头下领着两个及以上节点,就说明有形状形同的雪花。
时间复杂度:暴力枚举 $n$ 个雪花,每一次都进入hash表中的某个链表查询,链表规模 $Oleft( {frac{n}{P}} ight)$(随机数据),总时间复杂度 $Oleft( {frac{{n^2 }}{P}} ight)$,当 $P$ 是一个和 $N$ 接近的质数时,时间复杂度接近 $Oleft( n ight)$。
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. "); return 0; } } printf("No two snowflakes are alike. "); }
用vector写邻接表被卡了……老老实实改用数组模拟……
以上是关于POJ 3349 - Snowflake Snow Snowflakes - [hash]的主要内容,如果未能解决你的问题,请参考以下文章
POJ 3349 -- Snowflake Snow Snowflakes
POJ 3349 Snowflake Snow Snowflakes
poj3349 Snowflake Snow Snowflakes(HASH)
哈希—— POJ 3349 Snowflake Snow Snowflakes