POJ 3349 Snowflake Snow Snowflakes(哈希) Posted on 2018-10-28 | In ACM | 阅读 先哈希,再对比,对比就是循环滚动对比 C++ Code 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869#include <iostream>#include <stdio.h>#include <algorithm>#include <string.h>#include <vector>#define MAX 100005#define KEY 100000using namespace std;int snowFlakes[MAX][6];vector<int> head[KEY];bool isSame(int a, int b){ for(int i = 0; i < 6; i++){ bool flag = true; for(int j = 0; j < 6; j++){ if(snowFlakes[a][j] != snowFlakes[b][(i+j)%6]){ flag = false; break; } } if(flag == true) return true; } for(int i = 0; i < 6; i++){ bool flag = true; for(int j = 0; j < 6; j++){ if(snowFlakes[a][j] != snowFlakes[b][(i-j+6)%6]){ flag = false; break; } } if(flag == true) return true; } return false;}int main(){ int num, sum; bool flag = false; scanf("%d", &num); for(int i = 0; i < num; i++){ sum = 0; for(int j = 0; j < 6; j++){ scanf("%d", &snowFlakes[i][j]); sum += snowFlakes[i][j]; } if(flag == false){ int key = sum % KEY; if(head[key].size() > 0){ for(int j = 0; j < head[key].size(); j++){ if(isSame(head[key][j], i)){ flag = true; break; } } } head[key].push_back(i); } } if(flag){ printf("Twin snowflakes found.\n"); }else{ printf("No two snowflakes are alike.\n"); } return 0;}