标签:turn 起点 oid double tin ret std set fine
题意:给定n(0 < n ≤ 100000)个雪花,每个雪花有6个花瓣(花瓣具有一定的长度),问是否存在两个相同的雪花。若两个雪花以某个花瓣为起点顺时针或逆时针各花瓣长度依次相同,则认为两花瓣相同。
分析:
1、按雪花各花瓣长度之和对MOD取余哈希。对各雪花,算出哈希值,在哈希表中查询是否有与之相同的花瓣。
2、每个花瓣以某个花瓣为起点顺时针或逆时针共有12种表示方法。
3、注意哈希表中只需保存雪花输入时给定的那组长度值即可。
在哈希表中查询时,再算出该组长度值所对应的12种表示,并一一比较,若将这12种表示提前算好,并存入哈希表,则会超时。
#include<cstdio> #include<cstring> #include<cstdlib> #include<cctype> #include<cmath> #include<iostream> #include<sstream> #include<iterator> #include<algorithm> #include<string> #include<vector> #include<set> #include<map> #include<stack> #include<deque> #include<queue> #include<list> #define lowbit(x) (x & (-x)) const double eps = 1e-8; inline int dcmp(double a, double b){ if(fabs(a - b) < eps) return 0; return a > b ? 1 : -1; } typedef long long LL; typedef unsigned long long ULL; const int INT_INF = 0x3f3f3f3f; const int INT_M_INF = 0x7f7f7f7f; const LL LL_INF = 0x3f3f3f3f3f3f3f3f; const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f; const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1}; const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1}; const int MOD = 20000; const double pi = acos(-1.0); const int MAXN = 20000 + 10; const int MAXT = 1e6 + 10; using namespace std; int a[12]; struct Node{ int x[6]; void read(){ for(int i = 0; i < 6; ++i) scanf("%d", &x[i]); } }; vector<Node> v[MAXN]; int Hash(Node& tmp){ int ans = 0; for(int i = 0; i < 6; ++i){ (ans += tmp.x[i] % MOD) %= MOD; } return ans; } bool judge1(int *q, int *w){ for(int i = 0; i < 6; ++i){ if(q[i] != w[i]) return false; } return true; } bool judge(Node& q, Node& w){ int tmp[12]; int t[6]; for(int i = 0; i < 6; ++i) tmp[i] = tmp[i + 6] = q.x[i]; for(int i = 0; i < 6; ++i){ for(int j = i; j < 6 + i; ++j){ t[j - i] = tmp[j]; } if(judge1(t, w.x)) return true; } for(int i = 11; i >= 6; --i){ for(int j = i; j >= i - 5; --j){ t[i - j] = tmp[j]; } if(judge1(t, w.x)) return true; } return false; } int main(){ int n; scanf("%d", &n); bool ok = false; while(n--){ int sum = 0; Node tmp; tmp.read(); if(ok) continue; int id = Hash(tmp); int len = v[id].size(); v[id].push_back(tmp); if(len){ for(int i = 0; i < len; ++i){ if(judge(v[id][i], tmp)){ ok = true; break; } } } } if(ok){ printf("Twin snowflakes found.\n"); } else{ printf("No two snowflakes are alike.\n"); } return 0; }
POJ - 3349 Snowflake Snow Snowflakes (哈希)
标签:turn 起点 oid double tin ret std set fine
原文地址:http://www.cnblogs.com/tyty-Somnuspoppy/p/6653801.html