poj3349(hash or violence)
| Time Limit: 4000MS | Memory Limit: 65536K | |
| Total Submissions: 38600 | Accepted: 10120 |
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
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<ctime>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<stack>
#include<queue>
using namespace std;
#define inf 2000000000
#define linf 99999999999999
#define ll long long
#define N 1000010
#define mod 999983
#define md 10003
#define mx 100037
#define debug(x) cout<<"debug: "<<x<<endl
inline const int read(){
register int x=,f=;
register char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
inline const char in(){
for(register char ch=getchar();;ch=getchar()) if(ch>='A'&&ch<='Z') return ch;
}
int n,a[];
ll w[N];
void deal(int k){
ll res=,ans=;
//for(int i=6;i;i--) res=res+a[i]%mod;
//res=res%mod+1;
for(int i=;i;i--) res=res+a[i]%mx;
for(int i=;i;i--) ans=ans*a[i]%mod;
w[k]=(res*md%mod+)*(ans*md%mod+);
}
int main(){
//freopen("sh.txt","r",stdin);
n=read();
for(int i=;i<=n;i++){
//memset(a,-1,sizeof a);
for(int j=;j<=;j++) a[j]=read();
deal(i);
}
stable_sort(w+,w+n+);
int t=unique(w+,w+n+)-(w+);
//for(int i=t;i<=n;i++) printf("%d %d\n",i,w[i]);
if(t<n) puts("Twin snowflakes found.");
else puts("No two snowflakes are alike.");
return ;
}
上述代码,hash离散化程度太高,导致数据较大时,基本上匹配不上。
所以干脆不用hash,裸暴力就可以了。
思路清晰,看代码就好了。
AC代码1:
//请用c++提交
#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define N 100010
struct node{
int e[];
void is(){
sort(e,e+);
}
void get(){
scanf("%d%d%d%d%d%d",&e[],&e[],&e[],&e[],&e[],&e[]);
}
}snow[N];
int n;
bool flag;
bool cmp(const node &a,const node &b){
for(int i=;i<;i++){
if(a.e[i]==b.e[i]) continue;
return a.e[i]<b.e[i];
}
return flag=;
}
int main(){
scanf("%d",&n);
for(int i=;i<n;i++){
snow[i].get();
snow[i].is();
}
flag=;
sort(snow,snow+n,cmp);
if(flag) printf("Twin snowflakes found.\n");
else printf("No two snowflakes are alike.\n");
return ;
}
当然你非要hash的代码,shenben也奉上,但不做解释。
AC代码2:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=;
const int H=;
struct Node{
int num[];
int next;
}node[N];
int cur;
int hashTable[H];
unsigned int getHash(int* num){
unsigned int hash=;
for(int i=;i<;++i) hash+=num[i];
return hash%H;
}
bool cmp(int* num1,int* num2){
for(int i=;i<;++i)
if(num1[i]!=num2[i]) return false;
return true;
} void insertHash(int* num,unsigned int h){
for(int i=;i<;++i) node[cur].num[i]=num[i];
node[cur].next=hashTable[h];
hashTable[h]=cur;
++cur;
} bool searchHash(int* num){
unsigned h=getHash(num);
int next=hashTable[h];
while(next!=-){
if(cmp(num,node[next].num)) return true;
next=node[next].next;
}
insertHash(num,h);
return false;
}
int main(){
int num[][];
int n;
bool twin=false;
memset(hashTable,-,sizeof hashTable);
scanf("%d",&n);
while(n--){
for(int i=;i<;i++) scanf("%d",&num[][i]),num[][i+]=num[][i];
if(twin) continue;
for(int i=;i<;i++) num[][i+]=num[][i]=num[][-i];
for(int i=;i<;i++){
if(searchHash(num[]+i)||searchHash(num[]+i)){
twin=true;
break;
}
}
}
if(twin) printf("Twin snowflakes found.\n");
else printf("No two snowflakes are alike.\n");
return ;
}
poj3349(hash or violence)的更多相关文章
- 题目1022:游船出租(hash简单应用)
问题来源 http://ac.jobdu.com/problem.php?pid=1022 问题描述 每次输入:船号(1~100) 键值(S或E) 发生时间(小时:分钟).当船号为0时,代表一天结束: ...
- sdoi2013 spring(hash+容斥)
大体思路是先求出来\(f[i]\)代表有至少\(i\)个位置相同的点对数. 然后就已经没什么好害怕的了(跟BZOJ3622一样) 然后这个\(f[i\)]怎么求呢? 最无脑的方法就是枚举位置,然后\( ...
- 字符串学习总结(Hash & Manacher & KMP)
前言 终于开始学习新的东西了,总结一下字符串的一些知识. NO.1 字符串哈希(Hash) 定义 即将一个字符串转化成一个整数,并保证字符串不同,得到的哈希值不同,这样就可以用来判断一个该字串是否重复 ...
- HDU - 3973 AC's String(Hash+线段树)
http://acm.hdu.edu.cn/showproblem.php?pid=3973 题意 给一个词典和一个主串.有两种操作,查询主串某个区间,问这主串区间中包含多少词典中的词语.修改主串某一 ...
- 牛客训练三:处女座的比赛(hash打表)
题目链接:传送门 思路:由于MOD只有9983大小,所以四位小写字母的字符串组合有26+26^2+26^3+26^4=475254种组合. 所以只要每次枚举出从1到475254中的hash值对应的字符 ...
- 哈希与位图(Hash and BitMap)
Hash:哈希机制 BitMap:位图机制 目的:都是为了保证检索方便而设置的数据结构 对于大数据进行排序,由于内存限制,不可能在内存中进行,所以采取BitMap机制 为了在大数据中快速检索以及操作数 ...
- 7-14 电话聊天狂人(25 分)(Hash表基本操作)
7-14 电话聊天狂人(25 分) 给定大量手机用户通话记录,找出其中通话次数最多的聊天狂人. 输入格式: 输入首先给出正整数N(≤105),为通话记录条数.随后N行,每行给出一条通话记录.简单 ...
- bzoj2124 等差子序列(hash+线段树)
2124: 等差子序列 Time Limit: 3 Sec Memory Limit: 259 MBSubmit: 719 Solved: 261[Submit][Status][Discuss] ...
- nyoj--138--找球号(二)(hash+邻接表)
找球号(二) 时间限制:1000 ms | 内存限制:65535 KB 难度:5 描述 在某一国度里流行着一种游戏.游戏规则为:现有一堆球中,每个球上都有一个整数编号i(0<=i<=1 ...
随机推荐
- .NET重构(五):存储过程、触发器和函数的区别
导读:在触发器的学习过程中,师傅讲了它的耦合性高,建议我能用存储过程,那到底什么是存储过程呢,自己也不是特别了解,还有就是,触发器也算是一种特殊的存储过程,为什么就不建议多用呢?接下来,就谈谈触发器. ...
- iOS长按控件
前言 网上看到一个button的长按控件效果不错,一个菱形从中间向两边增大,研究了下 原理 上图红色是控件上面放了视图,从上到下分别是view,normalLable,highlightLabel,b ...
- 论文笔记:Ten years of pedestrian detection, what have we learned?
最近正在研究行人检测,学习了一篇2014年发表在ECCV上的一篇综述性的文章,是对行人检测过去十年的一个回顾,从dataset,main approaches的角度分析了近10年的40多篇论文提出的方 ...
- response.sendRedirect()使用注意事项
用response.sendRedirect做转向其实是向浏览器发送一个特殊的Header,然后由浏览器来做转向,转到指定的页面,所以用sendRedirect时,浏览器的地址栏上可以看到地址的变化. ...
- ASP.NET里创建Microsoft Word文档
原文发布时间为:2008-08-03 -- 来源于本人的百度文章 [由搬家工具导入] 本文是应在ASP.NET里创建Microsoft Word文档之需而写的。这篇文章演示了在ASP.NET里怎么创建 ...
- raspi集成库及安装
原文:http://blog.csdn.net/xukai871105/article/details/12684617 树莓派来自国外,国外嵌入式开源领域具有良好的分享精神,树莓派各种集成库也层 ...
- java 基础 5 String StringBuffer StringBuilder
String是不可变的,原因 1是可以缓存hash值,因为String的hash值经常被使用,例如String用作HashMap等.不可变特性 使得hash值不变,因此只需要进行一次计算: 2Str ...
- android应用开发之View的大小计量单位(px、dpi、dp、dip、sp)
http://blog.csdn.net/ljianhui/article/details/43601495?ref=myread 一.像素(px)与屏幕分辨率 1)px(Pixels ,像素):对应 ...
- 【js html】对于<img>图片的引用填充,src可以给什么值?
平时多见的<img>的使用,常见于如下: <img class="img-responsive img-rounded" src="static/img ...
- Go -- 交叉编译
编译Windows程序和mac程序