hash应用以及vector的使用简介:POJ 3349 Snowflake Snow Snowflakes
今天学的hash。说实话还没怎么搞懂,明天有时间把知识点总结写了,今天就小小的写个结题报告吧!
题意:
在n (n<100000)个雪花中判断是否存在两片完全相同的雪花,每片雪花有6个角,每个角的长度限制为1000000
两片雪花相等的条件:
雪花6个角的长度按顺序相等(这个顺序即可以是顺时针的也可以是逆时针的)
解题思路:
hash:连加求余法 求key 值,链地址法解决冲突,连加求余法 求key 值挺简单,关于链地址法解决冲突可以通过c++中,vector容器可以较为方便的实现。
下面先介绍一下vector容器:(其实今天也第一次接触vector容器)
1 基本操作
(1)头文件#include<vector>.
(2)创建vector对象,vector<int> vec;
(3)尾部插入数字:vec.push_back(a);
(4)使用下标访问元素,cout<<vec[0]<<endl;记住下标是从0开始的。
(5)使用迭代器访问元素.
vector<int>::iterator it;
for(it=vec.begin();it!=vec.end();it++)
cout<<*it<<endl;
(6)插入元素: vec.insert(vec.begin()+i,a);在第i+1个元素前面插入a;
(7)删除元素: vec.erase(vec.begin()+2);删除第3个元素
vec.erase(vec.begin()+i,vec.end()+j);删除区间[i,j-1];区间从0开始
(8)向量大小:vec.size();
(9)清空:vec.clear();
具体思路:
当且仅当两片雪花的key值一样时,这两片雪花才有可能相同。
求出一组的key值后,在该key值的容器中寻找是否有相同的雪花若存在,break出来即可。
那么当我们寻找到key值相同的两片雪花时,我们该如何比较两片雪花?
其实是可以的。假设有两片雪花,A 和B
我们固定A,先按顺时针方向比较:
若A0==B0,则按顺序比较A1和B1.........比较A5和B5
只要当出现Ai != Bi,则把B顺时针转动一次,
若A0==B1,则按顺序比较A1和B2.........比较A5和B0
以此类推,直至B转动了5次,若还不相同,则说明这两片雪花在顺时针方向不等。
再比较逆时针方向:
同样固定A,若A0==B5,则按顺序比较A1和B4.........比较A5和B0
只要当出现Ai != B(5-i),则把B逆时针转动一次,
若A0==B4,则按顺序比较A1和B3.........比较A5和B5
以此类推,直至B转动了5次,若还不相同,则说明这两片雪花在逆时针方向不等。
听了这么多,小盆友们是不是都会了呢?嘿嘿,附上代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
#define maxn 100010
#define pri 100007
vector<int> hash[pri];
int snow[maxn][];
int n;
int cmp(int a,int b)
{
for(int i=; i<; i++)
{
if(snow[a][]==snow[b][i%] )
if(snow[a][]==snow[b][(i+)%] && snow[a][]==snow[b][(i+)%] && snow[a][]==snow[b][(i+)%] && snow[a][]==snow[b][(i+)%] && snow[a][]==snow[b][(i+)%])
return ;
if(snow[a][]==snow[b][i%])
if(snow[a][]==snow[b][(i+)%] && snow[a][]==snow[b][(i+)%] && snow[a][]==snow[b][(i+)%] && snow[a][]==snow[b][(i+)%] && snow[a][]==snow[b][(i+)%])
return ;
}
return ;
}
int main()
{
scanf("%d",&n);
int i,j;
for(i=; i<n; i++)
{
scanf("%d%d%d%d%d%d",&snow[i][],&snow[i][],&snow[i][],&snow[i][],&snow[i][],&snow[i][]);
}
int sum,flag=,key;
for(i=; i<n&&flag!=; i++)
{
sum=;
for(j=; j<; j++)
sum+=snow[i][j];
key=sum%pri;
for(j=; j<hash[key].size(); j++)
{
if(cmp(i,hash[key][j]))
{
flag=;
puts("Twin snowflakes found.");
break;
}
}
hash[key].push_back(i);
}
if(flag==)
puts("No two snowflakes are alike.");
return ;
}
There is a meaning for wings that cannot fly,it's a previous memory of when you once flew through the sky.
hash应用以及vector的使用简介:POJ 3349 Snowflake Snow Snowflakes的更多相关文章
- POJ 3349 Snowflake Snow Snowflakes (Hash)
Snowflake Snow Snowflakes Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 48646 Accep ...
- POJ 3349 Snowflake Snow Snowflakes
Snowflake Snow Snowflakes Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 27598 Accepted: ...
- [ACM] POJ 3349 Snowflake Snow Snowflakes(哈希查找,链式解决冲突)
Snowflake Snow Snowflakes Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 30512 Accep ...
- 哈希—— POJ 3349 Snowflake Snow Snowflakes
相应POJ题目:点击打开链接 Snowflake Snow Snowflakes Time Limit: 4000MS Memory Limit: 65536K Total Submissions ...
- POJ 3349 Snowflake Snow Snowflakes(简单哈希)
Snowflake Snow Snowflakes Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 39324 Accep ...
- POJ 3349 Snowflake Snow Snowflakes Hash
题目链接: http://poj.org/problem?id=3349 #include <stdio.h> #include <string.h> #include < ...
- [poj 3349] Snowflake Snow Snowflakes 解题报告 (hash表)
题目链接:http://poj.org/problem?id=3349 Description You may have heard that no two snowflakes are alike. ...
- POJ 3349 Snowflake Snow Snowflakes(哈希)
http://poj.org/problem?id=3349 题意 :分别给你n片雪花的六个角的长度,让你比较一下这n个雪花有没有相同的. 思路:一开始以为把每一个雪花的六个角的长度sort一下,然后 ...
- POJ 3349 Snowflake Snow Snowflakes (哈希表)
题意:每片雪花有六瓣,给出n片雪花,六瓣花瓣的长度按顺时针或逆时针给出,判断其中有没有相同的雪花(六瓣花瓣的长度相同) 思路:如果直接遍历会超时,我试过.这里要用哈希表,哈希表的关键码key用六瓣花瓣 ...
随机推荐
- AOP 面向方面的编程 Aspect Oriented Programing --第一次听说呢,汗!
http://wayfarer.cnblogs.com/articles/241024.html 抄在这里,方便复习! AOP技术基础 1.引言2.AOP技术基础 3.Java平台AOP技术研究4 ...
- Data Annotation
Data Annotation 什么是Data Annotation ? 如何使用 ? 自定义Validate Attribute EF Db first中使用Data Annotation asp ...
- Qt 智能指针学习(7种QT智能指针和4种std智能指针)
从内存泄露开始? 很简单的入门程序,应该比较熟悉吧 ^_^ #include <QApplication> #include <QLabel> int main(int arg ...
- Android中通过耳机按键控制音乐播放的实现
今天在研究Android中实现Android 4.2.2源码中的Music应用的源码,关于通过耳机按键控制音乐播放的实现,有点好奇,就仔细分析了一下源码, 主要由 MediaButtonIntentR ...
- hdu 4861 Couple doubi(数论)
题目链接:hdu 4861 Couple doubi 题目大意:两个人进行游戏,桌上有k个球,第i个球的值为1i+2i+⋯+(p−1)i%p,两个人轮流取,假设DouBiNan的值大的话就输出YES, ...
- 记一次在Tomcat部署项目后无法启动该项目的例子
在一次正常部署后,发现在Tomcat的管理中无法将该项目启动起来,并在Tomcat的日志文件localhost.log中发现以下错误信息: java.lang.NoSuchMethodError: o ...
- 如何在Eclipse配置Tomcat服务器
链接地址:http://jingyan.baidu.com/article/3065b3b6efa9d7becff8a4c6.html 要想在Eclipse运行jsp文件,首先需要指定对应的服务器,即 ...
- Ember.js - About
Ember.js - About More Productive Out of the Box. Write dramatically less code with Ember's Handleb ...
- Android原生APP内分享
Android原生APP内分享,可实现数据分享以及assets文件夹分享及私有文件分享 项目地址:https://github.com/json-pu/AndroidAppShare.git
- Qt QDebug等重定向到文件中
源地址:http://blog.163.com/soyo_gogogo/blog/static/171414077201392705639321/ Qt的qDebug() 等函数,可以打印出十分细致的 ...