题目

很简单,给一堆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;
}

最新文章

  1. VS2013环境问题
  2. html5手写签名
  3. MC的分布式算法的实现和一些总结
  4. WPF登陆窗口、主窗口切换问题
  5. 【poj1010】 STAMPS
  6. SPRING IN ACTION 第4版笔记-第十章Hitting the database with spring and jdbc-002-本章的源代码
  7. C++类继承内存布局(二)
  8. Hibernate分页
  9. oracle中LAG()和LEAD()等分析统计函数的使用方法(统计月增长率)
  10. Fitnesse用系列三
  11. 因下面文的损坏或丢失windows/system32/config/system 解决方法
  12. Unity3d中的PlayerPrefs游戏存档API的扩展
  13. LeetCode 542. 01 Matrix
  14. Linux运维老司机:CentOS6.9配置安装并配置Rsync
  15. 为什么LINQ to XML的性能要优于XmlDocument?
  16. pandas 将excel一列拆分成多列重新保存
  17. 如何在CentOS 7.2上创建NFS的Share,然后让Client可以访问
  18. 转:D3DXVec3TransformNormal() 与 3DXVec3TransformCoord() 的区别
  19. 监督学习(Supervised learning)
  20. Linux服务器修改时区时间

热门文章

  1. Problem N: 求二维数组中的鞍点【数组】
  2. 修改deeplabv3的test的输出的label颜色
  3. 【转】瓜娃(guava)的API快速熟悉使用
  4. java static block
  5. 线程的sleep方法
  6. PAT (Basic Level) Practise (中文)- 1009. 说反话 (20)
  7. cocos2dx lua 打印和保存日志
  8. C++ string头文件
  9. composer安装laravel-u-editor及其使用
  10. 初学Python02