poj 3349 Snowflake Snow Snowflakes【HASH】/【最小表示】

发现最小表示+sort好像还快一点点

HASH

#include<iostream> #include<vector> #include<cstdio> using namespace std; const int maxn=9997; struct node{ int a[6],sum; }; vector<node> v[maxn]; bool cmp(node b,node c) { if(b.sum!=c.sum) return 0; for(int j,i=0;i<6;i++) if(b.a[0]==c.a[i]) { for(j=1;j<6;j++) if(b.a[j]!=c.a[(i+j)%6]) break; if(j==6) return 1; for(j=1;j<6;j++) if(b.a[6-j]!=c.a[(i+j)%6]) break; if(j==6) return 1; } return 0; } bool find() { for(int i=0;i<maxn;i++) for(int j=0;j<v[i].size();j++) for(int k=j+1;k<v[i].size();k++) if(cmp(v[i][j],v[i][k])) return 1; return 0; } int read(int &d) { char ch; while(ch=getchar(),ch>57||ch<48); d=ch-48; while(ch=getchar(),ch<=57&&ch>=48) d=d*10+ch-48; return d; } int main() { int t;node k; read(t); while(t--) { k.sum=0; for(int i=0;i<6;i++) k.sum+=read(k.a[i]); v[k.sum%maxn].push_back(k); } puts(find()?"Twin snowflakes found.":"No two snowflakes are alike."); return 0; }

最小表示

#include<cstdio> #include<algorithm> #include<vector> using namespace std; const int maxn=100000+10; struct node { int s[6]; }a[maxn]; int read(int &d) { char ch; while(ch=getchar(),ch>57||ch<48); d=ch-48; while(ch=getchar(),ch<58&&ch>47) d=d*10+ch-48; return d; } int getmin(int a[],int len) { int p=0,q=1,k=0,cmp; while(p<len&&q<len&&k<len) { cmp=a[(p+k)%len]-a[(q+k)%len]; if(cmp==0){k++;continue;} if(cmp>0) p+=k+1; else q+=k+1; if(p==q) q++; k=0; } return min(p,q); } bool cmp(node a,node b) { for(int i=0;i<6;i++) if(a.s[i]!=b.s[i]) return a.s[i]<b.s[i]; return false; } int main() { int t,m,n,b[6],c[6],cnt=0; read(t); while(t--) { for(int i=0;i<6;i++) { c[5-i]=read(b[i]); } m=getmin(b,6),n=getmin(c,6); bool flag; for(int i=0;i<6;i++) if(b[(i+m)%6]>c[(i+n)%6]){flag=1;break;} else if(b[(i+m)%6]<c[(i+n)%6]){flag=0;break;} if(flag) for(int i=0;i<6;i++) a[cnt].s[i]=c[(i+n)%6]; else for(int i=0;i<6;i++) a[cnt].s[i]=b[(i+m)%6]; cnt++; } bool flag=false; sort(a,a+cnt,cmp); for(int i=1;i<cnt;i++) { bool flag2=true; for(int j=0;j<6;j++) if(a[i].s[j]!=a[i-1].s[j]) flag2=false; if(flag2){flag=true;break;} } puts(flag?"Twin snowflakes found.":"No two snowflakes are alike."); return 0; }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值