3349 hash 链表

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

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

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

  1. #include<iostream>
  2. usingnamespacestd;
  3. structnode
  4. {
  5. intflag;
  6. inta[6];
  7. node*next;
  8. }hash[150006];
  9. intfac(inttt[]);
  10. intcomp(inta[],intb[]);
  11. intmain()
  12. {
  13. freopen("in.txt","r",stdin);
  14. intt[6];
  15. intn,i,sum,j;
  16. intflag(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. else
  34. {
  35. nodetemp=hash[sum];
  36. nodetemp1;
  37. temp1.flag=0;
  38. temp1.next=NULL;
  39. memcpy(temp1.a,t,sizeof(t));
  40. while(1)
  41. {
  42. if(comp(temp.a,t))
  43. {
  44. cout<<"Twinsnowflakesfound."<<endl;
  45. exit(-1);
  46. }
  47. if(temp.next==NULL)
  48. break;
  49. temp=*temp.next;
  50. }
  51. temp.next=&temp1;
  52. }
  53. }
  54. cout<<"Notwosnowflakesarealike."<<endl;
  55. return0;
  56. }
  57. intfac(inttt[])
  58. {
  59. __int64sum(0);
  60. inti;
  61. for(i=0;i<6;i++)
  62. sum+=tt[i];
  63. returnsum%149997;
  64. }
  65. intcomp(inta[],intb[])
  66. {
  67. inti,j,t;
  68. for(i=0;i<6;i++)
  69. if(a[i]==b[0])
  70. {
  71. if(i==6)
  72. return0;
  73. for(j=1;j<6;j++)//往前
  74. {
  75. if((i+j)>=6)
  76. t=(i+j)-6;
  77. else
  78. t=i+j;
  79. if(a[t]==b[j])
  80. continue;
  81. else
  82. break;
  83. }
  84. if(j==6)
  85. return1;
  86. for(j=1;j<6;j++)//往后
  87. {
  88. if(i-j<0)
  89. t=i-j+6;
  90. else
  91. t=i-j;
  92. if(a[t]==b[j])
  93. continue;
  94. else
  95. break;
  96. }
  97. if(j==6)
  98. return1;
  99. }
  100. return0;
  101. }
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值