poj3349
来源:互联网 发布:笛子校音器软件 编辑:程序博客网 时间:2024/05/22 13:13
题意:给n个长度为6的序列环,求是否有相同的环,能通过旋转和翻面而重合则相同。即顺时针或逆时针序列相同。
思路:直接暴力两两比较的话,时间复杂度O(6*6*n*n)超时了,用hash表牺牲内存来减少时间复杂度,没学过hash表的同学可以看这个博客,http://kb.cnblogs.com/page/189480/
#include <iostream>#include <vector>#include <cstdio>using namespace std;const int maxn = 100000+10;const int mod = 10997;typedef struct{ int a[6];}node;vector<node> arr[mod+1];int getHash(node y){ int value = 0; for(int i = 0;i<6;i++) { value += y.a[i]%mod; value %= mod; } return value;}bool cmp(node x,int value){ for(int i = 0;i<arr[value].size();i++) { for(int j = 0;j<6;j++) { int k; int l = 0; for(k = 0;k<6;k++) { if(arr[value][i].a[l++]!=x.a[(k+j)%6]) break; } if(k==6) return true; l = 0; for(k = 5;k>=0;k--) { if(arr[value][i].a[l++]!=x.a[(k+j)%6]) break; } if(k==-1) return true; } } return false;}int main(){ int n; scanf("%d",&n); int gg = 0; for(int i = 0;i<n;i++) { node tem; for(int j = 0;j<6;j++) { scanf("%d",&tem.a[j]); } if(gg) continue; int hashValue = getHash(tem); if(cmp(tem,hashValue)) { gg = 1; } arr[hashValue].push_back(tem); } if(gg) printf("Twin snowflakes found."); else printf("No two snowflakes are alike."); return 0;}
阅读全文
1 0
- poj3349
- poj3349
- POJ3349
- POJ3349
- poj3349
- poj3349
- POJ3349
- POJ3349
- poj3349
- poj3349
- POJ3349
- poj3349
- POJ3349
- poj3349
- POJ3349 哈希表
- POJ3349 #no
- poj3349-hash
- poj3349--Hash
- Android传感器 设备坐标系到世界坐标系的转换 分析以及应用
- 8、pcb绘制过程简介
- SWJTUOJ-2364 A room Problem(easy)
- thinkphp整合系列之苹果AppStore内购付款的服务器端php验证
- 数字图像处理实验(17):PROJECT 06-04,Color Image Segmentation
- poj3349
- 9、铺铜铺地说明
- exists子查询真的就比连接查询慢吗?
- Hello world终于可以显示了
- 2017 计蒜之道 初赛 第三场
- 关于ros通信机制的浅谈
- 10、整体检查开板
- Qt-QML-关于两个平级的qml文件中的函数调用问题
- 【B/S】系统登录