poj3349 find the same snowflake (hash)

topic portal

The main idea of ​​the title: give you n snowflakes, each of which has its own length for the six edges. If there are two snowflakes with the same length of each edge, output an English sentence, if not, output another English sentence, n and length are relatively large.

Idea: The first time I really came into contact with hashing, I checked the solution to the problem, but I have not been able to get the essence of hashing through this problem. This problem is to add up the six lengths of the snowflakes and take the modulo value as the hash value, and then classify the snowflakes with the same hash value into one category. Every time a new snowflake is input, find its category first. , and then traverse in this class. The way of traversing is to brute force in a ring. My understanding of using hashing for this question is to use hash values ​​for classification (a bit like union search), in the class Find, reduce the number of traversals.

The code has detailed comments.

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<bitset>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#define INF 0x3f3f3f3f
#define CLR(x,y) memset(x,y,sizeof(x))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
using namespace std;
typedef pair<int,int> pii;
typedef long long ll;
const double PI=acos(-1.0);
int fact[10]= {1,1,2,6,24,120,720,5040,40320,362880};
const int mod = 999983;
const int maxn = 100005;
int tot = 0,n,head[mod+5],v[maxn][6],next[maxn];


int gethash(int id) { //To get the hash value, add up each bit and take the remainder
	int hashval=0;
	for(int i=0; i<6; i++) {
		hashval=(hashval%mod+v[id][i]%mod)%mod;
	}
	return hashval;
}
void addvhead(int hashval) { //adjacency list
	next[tot]=head[hashval];//head indicates the label of the last element whose six-digit sum is hashval next[i] indicates what the last representation of i is
	head[hashval]=tot++;//Update head tot is the currently added id
}
bool compare(int id,int x) {
	for(int i=0; i<6; i++) { //Compare clockwise
		int f=0;
		for(int st=i,j=0; j<6; j++,st= st+1 >= 6 ? 0 : st+1) {
			if(v[id][st]==v[x][j])f++;
			else break;
		}
		if(f==6)return true;
	}
	for(int i=0; i<6; i++) { //逆时针
		int f=0;
		for(int st=i,j=5; j>=0; j--,st= st+1 >= 6 ? 0 : st+1) {
			if(v[id][st]==v[x][j])f++;
			else break;
		}
		if(f==6)return true;
	}
	return false;
}
bool pd(int id) {
	int hashval=gethash(id);
	for(int i=head[hashval]; i!=-1; i=next[i]) { //Iterate over snowflakes with the same hash value
		if(compare(id,i))return true;
	}
	addvhead(hashval);

	return false;
}
int main() {
	memset(head,-1,sizeof(head));
	cin>>n;
	bool flag=false;
	for(int i=0; i<n; i++) {
		for(int j=0; j<6; j++)scanf("%d",&v[i][j]);
		if(flag)continue;
		if(pd(i))flag=true;

	}
	if(flag)cout<<"Twin snowflakes found."<<endl;
	else cout<<"No two snowflakes are alike."<<endl;
}
Snowflake Snow Snowflakes
Time Limit: 4000MS   Memory Limit: 65536K
Total Submissions: 47318   Accepted: 12345

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

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326662540&siteId=291194637