poj3349 Snowflake Snow Snowflakes【HASH】
Time Limit: 4000MS | Memory Limit: 65536K | |
Total Submissions: 49991 | Accepted: 13040 |
Description
You may have heard that no two snowflakes are alike. Your task is to write a program to determine whether this is really true. Your program will read information about a collection of snowflakes, and search for a pair that may be identical. Each snowflake has six arms. For each snowflake, your program will be provided with a measurement of the length of each of the six arms. Any pair of snowflakes which have the same lengths of corresponding arms should be flagged by your program as possibly identical.
Input
The first line of input will contain a single integer n, 0 < n ≤ 100000, the number of snowflakes to follow. This will be followed by n lines, each describing a snowflake. Each snowflake will be described by a line containing six integers (each integer is at least 0 and less than 10000000), the lengths of the arms of the snow ake. The lengths of the arms will be given in order around the snowflake (either clockwise or counterclockwise), but they may begin with any of the six arms. For example, the same snowflake could be described as 1 2 3 4 5 6 or 4 3 2 1 6 5.
Output
If all of the snowflakes are distinct, your program should print the message:
No two snowflakes are alike.
If there is a pair of possibly identical snow akes, your program should print the message:
Twin snowflakes found.
Sample Input
2
1 2 3 4 5 6
4 3 2 1 6 5
Sample Output
Twin snowflakes found.
Source
题意:
一朵雪花有六条边,给n朵雪花,问其中有没有相同的雪花。当两个雪花的边都对应相同时,雪花是相同的。
思路:
,其中P是我们选定的一个较大的质数
这个HASH函数把所有的雪花分成了P类,对于每一朵 雪花,定位到head[]这个表头所指向的链表。如果该链表中不包含和这朵雪花相同的雪花,就在表头后插入一个新节点(对于一些题目可以在该节点上记录出现了的次数)。
#include <iostream>
#include <set>
#include <cmath>
#include <stdio.h>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
typedef long long LL;
#define inf 0x7f7f7f7f const int maxn = 1e6 + ;
int snow[maxn][], head[maxn], nxt[maxn];
int n, tot, P = ; int H(int *a)
{
int sum = , mul = ;
for(int i = ; i < ; i++){
sum = (sum + a[i]) % P;
mul = (long long)mul * a[i] % P;
}
return (sum + mul) % P;
} bool eequal(int *a, int *b)
{
for(int i = ; i < ; i++){
for(int j = ; j < ; j++){
bool eq = ;
for(int k = ; k < ; k++){
//cout<<a[(i + k) % 6]<<" "<<b[(i + k) % 6]<<endl;
if(a[(i + k) % ] != b[(j + k) % ]) eq = ;
}
if(eq) return ;
eq = ;
for(int k = ; k < ; k++){
if(a[(i + k) % ] != b[(j - k + ) % ])eq = ;
}
if(eq)return ;
}
}
return ;
} bool insertt(int *a)
{
int val = H(a);
//cout<<val<<endl;
for(int i = head[val]; i; i = nxt[i]){
//cout<<2<<endl;
if(eequal(snow[i], a)){
//cout<<1<<endl;
return ;
}
}
++tot;
memcpy(snow[tot], a, * sizeof(int));
nxt[tot] = head[val];
head[val] = tot;
return ;
} int main()
{
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
scanf("%d", &n);
//tot = 0;
//memset(head, 0, sizeof(head));
for(int i = ; i <= n; i++){
int a[];
for(int j = ; j < ; j++){
scanf("%d", &a[j]);
}
if(insertt(a)){
printf("Twin snowflakes found.\n");
return ;
}
}
printf("No two snowflakes are alike.\n"); }
最新文章
- 【hihoCoder】1039 : 字符消除
- 配置项setOption -- title
- Linux (centos )下Nginx+PHP+MySQL配置——自己的lnmp配置
- 《C与指针》第十五章练习
- HTML可编辑的select
- Devexpress 汉化
- node入门 express ejs
- Python学习路程day6
- Mysql Binlog日志详解
- CentOS 下mysql ERROR&;n…
- 扫雷游戏制作过程(C#描述):第五节、菜单操作(续)
- mqtt paho ssl java端代码
- IIS与ASP.NET对请求的处理
- DecimalFormat格式化十进制数字
- netty源码解解析(4.0)-1 核心架构
- Docker 修改默认存储路径的一个方法
- day28 网络编程
- 【机器学习算法】Boostrapping算法
- 译:微软发布.NET应用架构指南草案
- 【刷题】BZOJ 3551 [ONTAK2010]Peaks加强版