3349 hash 链表

http://acm.pku.edu.cn/JudgeOnline/problem?id=3349

题意为判断是后有相同的雪花,雪花的花瓣可能是顺时针,或逆时针描述,故对hash值相同的还要进行比较,相同则推出,否则把该雪花加到该hash对应的链表上

 

code:(时间复杂度还是太高了)

 

  1. #include<iostream>
  2. using namespace std;
  3. struct node
  4. {
  5.     int flag;
  6.     int a[6];
  7.     node *next;
  8. }hash[150006];
  9. int fac(int tt[]);
  10. int comp(int a[],int b[]);
  11. int main()
  12. {
  13.     freopen("in.txt","r",stdin);
  14.     int t[6];
  15.     int n,i,sum,j;
  16.     int flag(1);
  17.     for( i=0; i<100001; i++)
  18.     {
  19.         hash[i].flag=0;
  20.         hash[i].next=NULL;
  21.     }
  22.     cin>>n;
  23.     for( i=0; i<n; i++)
  24.     {
  25.         for( j=0; j<6; j++)
  26.             scanf("%d",&t[j]);
  27.         sum=fac(t);
  28.         if(!hash[sum].flag)
  29.         {   
  30.             hash[sum].flag=1;
  31.             memcpy(hash[sum].a,t,sizeof(t));
  32.             
  33.         }   
  34.         else
  35.         {   
  36.             node temp=hash[sum];
  37.             node temp1;
  38.             temp1.flag=0;
  39.             temp1.next=NULL;
  40.             memcpy(temp1.a,t,sizeof(t));
  41.             while(1)
  42.             {
  43.                 if(comp(temp.a,t))
  44.                 {
  45.                     cout<<"Twin snowflakes found."<<endl;
  46.                     exit(-1);
  47.                 }
  48.                 if(temp.next==NULL)
  49.                     break;
  50.                 temp=*temp.next;
  51.             }
  52.             temp.next=&temp1;
  53.         }
  54.     }
  55.     cout<<"No two snowflakes are alike."<<endl;
  56.             
  57.             
  58.     return 0;
  59. }
  60. int fac(int tt[])
  61. {
  62.     __int64 sum(0);
  63.     int i;
  64.     for( i=0; i<6; i++)
  65.         sum+=tt[i];
  66.     return sum%149997;
  67. }
  68. int comp(int a[],int b[])
  69. {
  70.     int i,j,t;
  71.     for( i=0; i<6; i++)
  72.         if(a[i]==b[0])
  73.         {
  74.             if(i==6)
  75.                 return 0;
  76.             for( j=1; j<6; j++)//往前
  77.             {
  78.                 if((i+j)>=6)
  79.                     t=(i+j)-6;
  80.                 else
  81.                     t=i+j;
  82.                 if(a[t]==b[j])
  83.                     continue;
  84.                 else
  85.                     break;
  86.             }
  87.             if(j==6)
  88.                 return 1;
  89.             for( j=1; j<6; j++)//往后
  90.             {
  91.                 if(i-j<0)
  92.                     t=i-j+6;
  93.                 else
  94.                     t=i-j;
  95.                 if(a[t]==b[j])
  96.                     continue;
  97.                 else
  98.                     break;
  99.             }
  100.             if(j==6)
  101.                 return 1;
  102.         }
  103.     
  104.     return 0;
  105. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值