poj3349 哈希

这题目写了一上午,一直错,然后自己测试数据还都对。为什么呢,为什么呢,后来我才发现代码里有一行free(tmp)。。。在55行那里。。。

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <stdlib.h>
 4 #include <algorithm>
 5 #include <math.h>
 6 #define mod 999983
 7 using namespace std;
 8 struct node{
 9     int num[6];
10     node *next;
11 }p[mod];
12 int sign[mod];
13 int cmp_1(node *a,node *b,int pos){
14     int i,j=pos;
15     for(i=0;i<6;++i){
16         if(a->num[i]!=b->num[j])
17             return 0;
18         j=(j+1)%6;
19     }
20     return 1;
21 }
22 int cmp_2(node *a,node *b,int pos){
23     int i,j=pos;
24     for(i=0;i<6;++i){
25         if(a->num[i]!=b->num[j])
26             return 0;
27         j=(j-1+6)%6;
28     }
29     return 1;
30 }
31 int cmp(node *a,node *b){
32     int i,j;
33     for(i=0;i<6;++i){
34         if(cmp_1(a,b,i)==1||cmp_2(a,b,i)==1)
35             return 1;
36     }
37     return 0;
38 }
39 int main(){
40     node *tmp;
41     node *op;
42     int key;
43     int success;
44     tmp=(node *)malloc(sizeof(node));
45     int i,j,n,cnt;
46     while(~scanf("%d",&n)){
47         memset(sign,0,sizeof(sign));
48         success=0;
49         for(i=0;i<mod;++i){
50             p[i].next=NULL;
51             for(j=0;j<6;++j)    p[i].num[j]=0;
52         }
53 
54         for(cnt=0;cnt<n;++cnt){
55             //free(tmp);
56             key=0;
57             tmp=(node *)malloc(sizeof(node));
58             for(i=0;i<6;++i){
59                 scanf("%d",&tmp->num[i]);
60                 key+=tmp->num[i];
61                 key=key%mod;
62             }
63             if(success==1) continue;
64             tmp->next=NULL;
65             key=key%mod;
66             if(sign[key]==0){
67                 p[key]=*tmp;
68                 sign[key]=1;
69             }else if(sign[key]==1){
70                 op=&p[key];
71                 while(1){
72                     if(cmp(op,tmp)==1){
73                         success=1;
74                         break;
75                     }
76                     if(op->next==NULL)  break;
77                     op=op->next;
78                 }
79                 if(success) continue;
80                 op->next=tmp;
81             }
82         }
83         if(success==1) printf("Twin snowflakes found.
");
84         else           printf("No two snowflakes are alike.
");
85 
86     }
87     return 0;
88 }
原文地址:https://www.cnblogs.com/symons1992/p/3487436.html