【POJ 3349】 Snowflake Snow Snowflakes
【题目链接】
http://poj.org/problem?id=3349
【算法】
哈希
若两片雪花相同,则它们六个角上的和一定相同,不妨令 H(A) = sigma(Ai) % P ,每次只要到哈希表里查是否出现相同雪花,即可
【代码】
#include <algorithm> #include <bitset> #include <cctype> #include <cerrno> #include <clocale> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <exception> #include <fstream> #include <functional> #include <limits> #include <list> #include <map> #include <iomanip> #include <ios> #include <iosfwd> #include <iostream> #include <istream> #include <ostream> #include <queue> #include <set> #include <sstream> #include <stdexcept> #include <streambuf> #include <string> #include <utility> #include <vector> #include <cwchar> #include <cwctype> #include <stack> #include <limits.h> using namespace std; #define MAXN 100010 const int P = 1e5 + 7; int i,j,n,tot; int a[7],Head[P],nxt[MAXN],snow[MAXN][7]; inline int get() { int i,ret = 0; for (i = 1; i <= 6; i++) ret = (ret + a[i]) % P; return ret; } inline bool Insert() { int i; int Hash = get(); bool same; for (i = Head[Hash]; i; i = nxt[i]) { same = true; for (j = 1; j <= 6; j++) same &= (snow[i][j] == a[j]); if (same) return true; } tot++; nxt[tot] = Head[Hash]; for (i = 1; i <= 6; i++) snow[tot][i] = a[i]; Head[Hash] = tot; return false; } int main() { scanf("%d",&n); for (i = 1; i <= n; i++) { for (j = 1; j <= 6; j++) scanf("%d",&a[j]); sort(a+1,a+7); if (Insert()) { printf("Twin snowflakes found.\n"); return 0; } } printf("No two snowflakes are alike.\n"); return 0; }