POJ3349 Snowflake Snow Snowflakes 【哈希表】
2024-08-30 05:18:56
题目
很简单,给一堆6元组,可以从任意位置开始往任意方向读,问有没有两个相同的6元组
题解
hash表入门题
先把一个六元组的积 + 和取模作为hash值,然后查表即可
期望\(O(n)\)
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#define LL long long int
#define Redge(u) for (int k = h[u]; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
using namespace std;
const int maxn = 100010,maxm = 100005,INF = 1000000000,P = 100007;
inline int read(){
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
return out * flag;
}
int h[maxn],nxt[maxn],n,tot;
LL A[6],H[maxn][6];
bool cmp(LL A[],LL B[]){
for (int pos = 0; pos < 6; pos++){
bool flag = true;
for (int i = 0; i < 6; i++)
if (A[i] != B[(pos + i) % 6]){
flag = false; break;
}
if (flag) return true;
flag = true;
for (int i = 0; i < 6; i++)
if (A[i] != B[(pos - i + 6) % 6]){
flag = false; break;
}
if (flag) return true;
}
return false;
}
bool ins(){
LL s = 0,m = 1,v;
for (int i = 0; i < 6; i++)
s = (s + A[i]) % P,m = m * A[i] % P;
v = (s + m) % P;
for (int k = h[v]; k != -1; k = nxt[k])
if (cmp(H[k],A)) return true;
tot++;
for (int i = 0; i < 6; i++) H[tot][i] = A[i];
nxt[tot] = h[v]; h[v] = tot;
return false;
}
int main(){
memset(h,-1,sizeof(h));
n = read();
for (int i = 1; i <= n; i++){
for (int j = 0; j < 6; j++)
A[j] = read();
if (ins()) {puts("Twin snowflakes found."); return 0;}
}
puts("No two snowflakes are alike.");
return 0;
}
最新文章
- VS2013环境问题
- html5手写签名
- MC的分布式算法的实现和一些总结
- WPF登陆窗口、主窗口切换问题
- 【poj1010】 STAMPS
- SPRING IN ACTION 第4版笔记-第十章Hitting the database with spring and jdbc-002-本章的源代码
- C++类继承内存布局(二)
- Hibernate分页
- oracle中LAG()和LEAD()等分析统计函数的使用方法(统计月增长率)
- Fitnesse用系列三
- 因下面文的损坏或丢失windows/system32/config/system 解决方法
- Unity3d中的PlayerPrefs游戏存档API的扩展
- LeetCode 542. 01 Matrix
- Linux运维老司机:CentOS6.9配置安装并配置Rsync
- 为什么LINQ to XML的性能要优于XmlDocument?
- pandas 将excel一列拆分成多列重新保存
- 如何在CentOS 7.2上创建NFS的Share,然后让Client可以访问
- 转:D3DXVec3TransformNormal() 与 3DXVec3TransformCoord() 的区别
- 监督学习(Supervised learning)
- Linux服务器修改时区时间