Snowflake Snow Snowflakes (POJ - 3349 ,Hash表查找)

2024-03-30 13:58

本文主要是介绍Snowflake Snow Snowflakes (POJ - 3349 ,Hash表查找),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一.题目链接:

POJ-3349

二.题目大意:

有 n 片雪花,每片雪花有六个角,若六个角均相同,则称两片雪花相等.

雪花六个角的记录可能顺时针,可能逆时针,开始点不确定.

让你判断是否存在两片相同的雪花.

三.分析:

居然还有考 Hash 表的题 一直觉得数据结构课本很鸡肋

题目不难,这里只是存个板子.

哈希函数选用除留余数法,利用链地址法处理冲突.

四.代码实现:

#include <set>
#include <map>
#include <ctime>
#include <queue>
#include <cmath>
#include <stack>
#include <bitset>
#include <vector>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define eps 1e-8
#define lc k * 2
#define rc k * 2 + 1
#define pi acos(-1.0)
#define ll long long int
using namespace std;const int M = (int)3e5;
const int mod = 99991;
const int inf = 0x3f3f3f3f;int a[6], b[6];struct node
{int s[6];
}tmp;
vector <node> v[mod];int get_h()
{int sum = 0;for(int i = 0; i < 6; ++i)sum = (sum + a[i]) % mod;return sum % mod;
}bool Equal()
{for(int i = 0; i < 6; ++i){bool flag = 1;for(int j = 0; j < 6; ++j){if(a[j] != b[(i + j) % 6]){flag = 0;break;}}if(flag)return 1;}reverse(b, b + 6);for(int i = 0; i < 6; ++i){bool flag = 1;for(int j = 0; j < 6; ++j){if(a[j] != b[(i + j) % 6]){flag = 0;break;}}if(flag)return 1;}return 0;
}bool Insert()
{int h = get_h();int len = v[h].size();for(int i = 0; i < len; ++i){memcpy(b, v[h][i].s, sizeof(v[h][i].s));if(Equal())return 1;}memcpy(tmp.s, a, sizeof(a));v[h].push_back(tmp);return 0;
}/**
2
1 2 3 4 5 6
4 3 2 1 6 5
**/int main()
{int n;scanf("%d", &n);bool flag = 0;for(int i = 0; i < n; ++i){for(int j = 0; j < 6; ++j)scanf("%d", &a[j]);if(flag)continue;if(Insert())flag = 1;}if(flag)printf("Twin snowflakes found.\n");elseprintf("No two snowflakes are alike.\n");return 0;
}

 

这篇关于Snowflake Snow Snowflakes (POJ - 3349 ,Hash表查找)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/861672

相关文章

学习hash总结

2014/1/29/   最近刚开始学hash,名字很陌生,但是hash的思想却很熟悉,以前早就做过此类的题,但是不知道这就是hash思想而已,说白了hash就是一个映射,往往灵活利用数组的下标来实现算法,hash的作用:1、判重;2、统计次数;

hdu1043(八数码问题,广搜 + hash(实现状态压缩) )

利用康拓展开将一个排列映射成一个自然数,然后就变成了普通的广搜题。 #include<iostream>#include<algorithm>#include<string>#include<stack>#include<queue>#include<map>#include<stdio.h>#include<stdlib.h>#include<ctype.h>#inclu

康拓展开(hash算法中会用到)

康拓展开是一个全排列到一个自然数的双射(也就是某个全排列与某个自然数一一对应) 公式: X=a[n]*(n-1)!+a[n-1]*(n-2)!+...+a[i]*(i-1)!+...+a[1]*0! 其中,a[i]为整数,并且0<=a[i]<i,1<=i<=n。(a[i]在不同应用中的含义不同); 典型应用: 计算当前排列在所有由小到大全排列中的顺序,也就是说求当前排列是第

hdu1496(用hash思想统计数目)

作为一个刚学hash的孩子,感觉这道题目很不错,灵活的运用的数组的下标。 解题步骤:如果用常规方法解,那么时间复杂度为O(n^4),肯定会超时,然后参考了网上的解题方法,将等式分成两个部分,a*x1^2+b*x2^2和c*x3^2+d*x4^2, 各自作为数组的下标,如果两部分相加为0,则满足等式; 代码如下: #include<iostream>#include<algorithm

usaco 1.2 Milking Cows(类hash表)

第一种思路被卡了时间 到第二种思路的时候就觉得第一种思路太坑爹了 代码又长又臭还超时!! 第一种思路:我不知道为什么最后一组数据会被卡 超时超了0.2s左右 大概想法是 快排加一个遍历 先将开始时间按升序排好 然后开始遍历比较 1 若 下一个开始beg[i] 小于 tem_end 则说明本组数据与上组数据是在连续的一个区间 取max( ed[i],tem_end ) 2 反之 这个

poj 3974 and hdu 3068 最长回文串的O(n)解法(Manacher算法)

求一段字符串中的最长回文串。 因为数据量比较大,用原来的O(n^2)会爆。 小白上的O(n^2)解法代码:TLE啦~ #include<stdio.h>#include<string.h>const int Maxn = 1000000;char s[Maxn];int main(){char e[] = {"END"};while(scanf("%s", s) != EO

hdu 2602 and poj 3624(01背包)

01背包的模板题。 hdu2602代码: #include<stdio.h>#include<string.h>const int MaxN = 1001;int max(int a, int b){return a > b ? a : b;}int w[MaxN];int v[MaxN];int dp[MaxN];int main(){int T;int N, V;s

poj 1511 Invitation Cards(spfa最短路)

题意是给你点与点之间的距离,求来回到点1的最短路中的边权和。 因为边很大,不能用原来的dijkstra什么的,所以用spfa来做。并且注意要用long long int 来存储。 稍微改了一下学长的模板。 stack stl 实现代码: #include<stdio.h>#include<stack>using namespace std;const int M

poj 3259 uva 558 Wormholes(bellman最短路负权回路判断)

poj 3259: 题意:John的农场里n块地,m条路连接两块地,w个虫洞,虫洞是一条单向路,不但会把你传送到目的地,而且时间会倒退Ts。 任务是求你会不会在从某块地出发后又回来,看到了离开之前的自己。 判断树中是否存在负权回路就ok了。 bellman代码: #include<stdio.h>const int MaxN = 501;//农场数const int

poj 1258 Agri-Net(最小生成树模板代码)

感觉用这题来当模板更适合。 题意就是给你邻接矩阵求最小生成树啦。~ prim代码:效率很高。172k...0ms。 #include<stdio.h>#include<algorithm>using namespace std;const int MaxN = 101;const int INF = 0x3f3f3f3f;int g[MaxN][MaxN];int n