poj 3349
了解到hash表的一点皮毛
1 #include <iostream> 2 #include <string> 3 #include <vector> 4 #include <cstdlib> 5 #include <cmath> 6 #include <map> 7 #include <algorithm> 8 #include <list> 9 #include <ctime> 10 #include <set> 11 #include <cstring> 12 #include <queue> 13 #include <cstdio> 14 #include <stack> 15 using namespace std; 16 int oridata[100005][6]; 17 const int primenum=99991; 18 vector<int> dic[primenum]; 19 20 int str_hash(int pos) { 21 int start = 0; 22 for (int i = 0; i < 6; i++) { 23 start = (start + oridata[pos][i]) % primenum; 24 } 25 return start; 26 } 27 bool check_one(int vara[], int varb[]) { 28 int j; 29 for (int i = 0; i < 6; i++) { 30 for (j = 0; j < 6; j++) { 31 if (vara[j] != varb[((i + j) % 6)]) { 32 break; 33 } 34 } 35 if (j == 6) { 36 return true; 37 } 38 for (j = 0; j < 6; j++) { 39 if (vara[j] != varb[((i - j+6) % 6)]) { 40 break; 41 } 42 } 43 if (j == 6) { 44 return true; 45 } 46 47 } 48 return false; 49 } 50 51 bool check(int hval, int cur) { 52 int sz = dic[hval].size(); 53 bool exists; 54 for (int i = 0; i < sz; i++) { 55 exists = check_one(oridata[dic[hval][i]], oridata[cur]); 56 if (exists) { 57 return true; 58 } 59 } 60 return false; 61 } 62 int main() { 63 int n; 64 bool insertfaild = false; 65 scanf("%d", &n); 66 int exists = 0; 67 int hval; 68 int i; 69 for ( i= 0; i < n; i++) { 70 // for (int a = 0; a < 6; a++) { 71 // scanf("%d", &oridata[i][a]); 72 // }//这种方法慢一些,蛋疼啊 73 scanf("%d%d%d%d%d%d", &oridata[i][0],&oridata[i][1],&oridata[i][2],&oridata[i][3],&oridata[i][4],&oridata[i][5]); 74 hval = str_hash(i); 75 if (check(hval, i)) { 76 insertfaild = true; 77 break; 78 }else{ 79 dic[hval].push_back(i); 80 } 81 } 82 if (!insertfaild) { 83 cout << "No two snowflakes are alike." << endl; 84 } else { 85 cout << "Twin snowflakes found." << endl; 86 } 87 return 0; 88 }