POJ 3349-Snowflake Snow Snowflakes-字符串哈希

哈希后,对每片雪花对比6次。

 

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <vector>
 4 #include <iostream>
 5 #include <algorithm>
 6 
 7 using namespace std;
 8 
 9 const int maxn = 15010;
10 const int prime = 14997;
11 
12 struct flake
13 {
14     int arm[6];
15     bool operator == (const struct flake b) const
16     {
17         for(int i=0;i<6;i++)
18         {
19             int j;
20             for(j=0;j<6;j++)
21             {
22                 if(arm[(j+i)%6] != b.arm[j]) break;
23             }
24             if(j == 6) return true;
25         }
26         for(int i=0;i<6;i++)
27         {
28             int j;
29             for(j=0;j<6;j++)
30             {
31                 if(arm[(j+i)%6] != b.arm[(6+(-1*j))%6]) break;
32             }
33             if(j == 6) return true;
34         }
35         return false;
36     }
37 };
38 
39 struct flake hashtable[maxn][100];
40 
41 int Hash(struct flake a)
42 {
43     int ans=0;
44     for(int i=0;i<6;i++)
45     {
46         ans += a.arm[i]%prime;
47     }
48     return ans%prime;
49 }
50 
51 int N;
52 int conf[maxn];
53 
54 int main()
55 {
56     while(~scanf("%d",&N))
57     {
58         flake sn;
59         int flag = 0;
60         memset(conf,0,sizeof conf);
61         for(int i=0;i<N;i++)
62         {
63             for(int i=0;i<6;i++) scanf("%d",&sn.arm[i]);
64             if(flag) continue;
65             int key = Hash(sn);
66             //printf("key=%d\n",key);
67 
68             for(int i=0;i<conf[key];i++)
69             {
70                 if(hashtable[key][i] == sn) flag = 1;
71             }
72             if(!flag) hashtable[key][conf[key]++] = sn;
73 
74         }
75         if(flag) printf("Twin snowflakes found.\n");
76         else     printf("No two snowflakes are alike.\n");
77     }
78 }

 

posted @ 2016-03-15 21:56  Helica  阅读(321)  评论(0编辑  收藏  举报