Poj3349 Snowflake Snow Snowflakes hash+minimal notation

Snowflake Snow Snowflakes
Time Limit: 4000MS   Memory Limit: 65536K
Total Submissions: 47154   Accepted: 12300

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.

Source

CCC 2007

The general idea: first read the string and then hash it normally according to the sum and product. When judging whether it is a snowflake, add the minimum notation optimization. Because snowflakes can be both positive and negative, it is necessary to flip the string again and use the minimum notation to match it again.

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int SIZE = 100010;
const int P = 99991;
int snow[SIZE][10],str[10],next[SIZE],head[SIZE],tot=0;
int strr[20];
inline int min(int a,int b){return a < b ? a : b;}
inline int H()
{
	int sum=0,mul=1;
	for(int i=1;i<=6;i++)
	{
		sum=(sum+str[i])%P;
		mul=(long long)mul*str[i]%P;
	}
	return (sum+mul)%P;
}
inline int Get1(int mem[])
{
	int i=1,j=2,k;
	while(i<=6&&j<=6)
	{
		for(k=0;k<=6&&mem[i+k]==mem[j+k];k++);
		if(k==6)return 1;
		if(mem[i+k]>mem[j+k])
		{
			i=i+k+1;
			if(i==j)i++;
		}
		else 
		{
			j=j+k+1;
			if(j==i)j++;
		}
	
	}
		return min(i,j);
}

inline bool equal(int a[])
{
	int snowsnow[20];
	for(int i=1;i<=6;i++)
		snowsnow[i]=a[i],snowsnow[i+6]=a[i];
	int ans1=Get1(snowsnow);//snow[i] zheng
	int ans2=Get1(strr);//strr
	int anss=1;
	for(int i=ans1,j=ans2,cnt=1;cnt<=6;cnt++,i++,j++)
		if(snowsnow[i]!=strr[j])anss=0;
	if(anss)return true;

	 for(int i=1;i<=3;i++)
	 {
	 	int t=snowsnow[i];
	 	snowsnow[i]=snowsnow[6-i+1];
	 	snowsnow[6-i+1]=t;
	 }
	 for(int i=1;i<=6;i++)
	 	snowsnow[i+6]=snowsnow[i];
	 ans1=Get1(snowsnow);
	 ans2=Get1(strr);
	 anss=1;
	for(int i=ans1,j=ans2,cnt=1;cnt<=6;cnt++,i++,j++)
		if(snowsnow[i]!=strr
			[j])anss=0;
	if(anss)return true;
	return false;
}
inline bool ins()
{
	int val=H();
	for(int i=head[val];i;i=next[i])
		if(equal(snow[i]))return 1;
	++tot;
	for(int i=1;i<=6;i++)
		snow[tot][i]=str[i];
	next[tot]=head[val];
	head[val]=tot;
	return false;
}
int main()
{
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=6;j++)scanf("%d",&str[j]);
		for(int j=1;j<=6;j++)
			strr[j]=str[j],strr[j+6]=str[j];
		if(ins())
		{
			printf("Twin snowflakes found.\n");
			return 0;
		}
	}
	printf("No two snowflakes are alike.\n");
	return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324833193&siteId=291194637