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.
代码:
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<map>
using namespace std;
typedef unsigned long long ull;
const int HASH=1e6+7,MAXN=1e5+7;
struct HASHMAP{
int head[HASH],next[MAXN],size;
ull state[MAXN];
int f[MAXN];
void init(){size=0;memset(head,-1,sizeof(head));}
int insert(ull val,int _id){
int h=val%HASH,tmp;
for(int i=head[h];~i;i=next[i])
if(val==state[i]){tmp=f[i],f[i]=_id;return tmp;}
f[size]=_id;
state[size]=val;
next[size]=head[h];
head[h]=size++;
return 0;
}
}H;
int n,a[13];
ull p=13331;
void minpos(int n){
int i=1,j=2,k=1;
while(i<=n&&j<=n&&k<=n){
int t=a[(i+k)]-a[(j+k)];
if(t==0)++k;
else{
if(t>0)i+=k+1;
else j+=k+1;
if(i==j)++j;
k=0;
}
}
int t=min(i,j);
int tp[13];
for(int i=1;i<=12;++i)tp[i]=a[i];
reverse(a+1,a+13);
i=1,j=2,k=1;
while(i<=n&&j<=n&&k<=n){
int t=a[(i+k)]-a[(j+k)];
if(t==0)++k;
else{
if(t>0)i+=k+1;
else j+=k+1;
if(i==j)++j;
k=0;
}
}
int tt=min(i,j),f=0;
int pt[13];
for(int i=1;i<=12;++i)pt[i]=a[i];
for(int i=1;i<=6;++i)
if(tp[i+t-1]>pt[i+tt-1]){f=1;break;}
else if(tp[i+t-1]<pt[i+tt-1]){f=0;break;}
for(int i=1;i<=6;++i)a[i]=f?pt[i+tt-1]:tp[i+t-1];
}
int main(){
scanf("%d",&n);
int ok=0,cnt=0;
H.init();
while(n--){
for(int i=1;i<=6;++i)scanf("%d",&a[i]),a[i+6]=a[i];
if(ok)continue;
minpos(6);
ull Hash=a[1];
for(int i=2;i<=6;++i)Hash=Hash*p+a[i];
if(H.insert(Hash,++cnt))ok=1;
}
if(ok)return puts("Twin snowflakes found."),0;
puts("No two snowflakes are alike.");
return 0;
}
用二分Hash O(nlogn)找最小表示法
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
typedef unsigned long long ull;
const int N=1e5+3;
const int HASH=1e6+7,MAXN=1e5+7;
struct HASHMAP{//Hash表
int head[HASH],next[MAXN],size;
ull state[MAXN];
void init(){size=0;memset(head,-1,sizeof(head));}
int insert(ull val){
int h=val%HASH;
for(int i=head[h];~i;i=next[i])if(val==state[i])return 1;
state[size]=val;
next[size]=head[h];
head[h]=size++;
return 0;
}
}H;
int P[13],Q[13],R[13];
ull p=13331,pp,Hash[13],pm[13];
ull getseg(int l,int r){
return Hash[r]-Hash[l-1]*(pm[r-l+1]);
}
bool C(int i,int j,int n,int P[]){
int l=1,r=n;
while(l<r){
int mid=(l+r)>>1;
if(getseg(i,i+mid-1)==getseg(j,j+mid-1))l=mid+1;
else r=mid;
}
return P[i+l-1]>P[j+l-1];
}
int minpos(int P[],int n){//二分hash找最小表示法
for(int i=1;i<=n;++i)P[i+n]=P[i];
for(int i=1;i<=n<<1;++i)Hash[i]=Hash[i-1]*p+P[i];
int i=1,j=n;
while(i<j){
if(C(i,j,n,P))++i;
else --j;
}
return i;
}
int n,ok;
int main(){
pm[0]=1;
for(int i=1;i<=12;++i)pm[i]=pm[i-1]*p;
scanf("%d",&n);
H.init();
while(n--){
for(int i=1;i<=6;++i)scanf("%d",&P[i]),Q[7-i]=P[i];
if(ok)continue;
int k=minpos(P,6),m=minpos(Q,6),f=0;
ull has=0;
for(int i=0;i<6;++i)
if(P[i+k]>Q[i+m]){f=1;break;}
else if(P[i+k]<Q[i+m])break;
for(int i=1;i<=6;++i)R[i]=f?Q[i+m-1]:P[i+k-1];
for(int i=1;i<=6;++i)has=has*p+R[i];
if(H.insert(has))ok=1;
}
if(ok)return puts("Twin snowflakes found.");
puts("No two snowflakes are alike.");
return 0;
}