zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    POJ2711 Leapin' Lizards(最大流)
    POJ3308 Paratroopers(最小割/二分图最小点权覆盖)
    POJ3084 Panic Room(最小割)
    POJ3469 Dual Core CPU(最小割)
    POJ1815 Friendship(字典序最小最小割割边集)
    HDU3395 Special Fish(最大费用任意流)
    HDU5461 Largest Point(暴力)
    POJ3184 Ikki's Story I
    POJ1637 Sightseeing tour(判定混合图欧拉回路)
    伸展树模板
  • 原文地址:https://www.cnblogs.com/symons1992/p/3487436.html
Copyright © 2011-2022 走看看