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 ...
随机推荐
- POJ-3481 Double Queue,Treap树和set花式水过!
Double Queue 本打算学二叉树,单纯的二叉树感觉也就那几种遍历了, 无意中看到了这个题,然后就 ...
- Git 常用命令整理(持续更新)
#配置 git config --global user.name "Your Name" git config --global user.email "email@e ...
- 【bzoj1109】[POI2007]堆积木Klo 动态规划+树状数组
题目描述 Mary在她的生日礼物中有一些积木.那些积木都是相同大小的立方体.每个积木上面都有一个数.Mary用他的所有积木垒了一个高塔.妈妈告诉Mary游戏的目的是建一个塔,使得最多的积木在正确的位置 ...
- BZOJ 3925 [Zjoi2015]地震后的幻想乡 ——期望DP
我们只需要考虑$\sum F(x)P(x)$的和, $F(x)$表示第x大边的期望,$P(x)$表示最大为x的概率. 经过一番化简得到$ans=\frac{\sum T(x-1)}{m+1}$ 所以就 ...
- 刷题总结——弹飞绵羊(bzoj2002)
题目: Description 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一条直线摆上n个装置, ...
- 刷题总结——营业额统计(bzoj1588)
题目: Description 营业额统计 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司的账本,账本上记录了公司成 ...
- hdu 4430 Yukari's Birthday 枚举+二分
注意会超long long 开i次根号方法,te=(ll)pow(n,1.0/i); Yukari's Birthday Time Limit: 12000/6000 MS (Java/Others) ...
- jsp 时间格式
<%@ taglib prefix='fmt' uri="http://java.sun.com/jsp/jstl/fmt" %> <fmt:formatDate ...
- Unable to locate Attribute with the the given name [] on this ManagedType
最近在写Springboot+hibernate的项目,遇到这个问题 好坑,查了半天没发现我哪里配置错了,后来发现实体类声明字段大小写敏感,把声明字段改成小写就行了
- Python 基础教程之包和类的用法
Python 基础教程之包和类的用法 建立一个文件夹filePackage 在filePackage 文件夹内创建 __init__.py 有了 __init__.py ,filePackage才算是 ...