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 ...
随机推荐
- Git x SVN rebase事故
Git x SVN rebase事故 @author ixenos 2019-01-09 14:21:21 前言: 昨天在Git x SVN 中进行git svn dcommit的时候,提示需要再进行 ...
- POJ 2106-Boolean Expressions,双栈运用类似表达式求值!
Boolean Expressions 首先声明此题后台可能极水(毕竟这种数据不好造!).昨天写了一天却总是找不到bug,讨论区各种数据都过了,甚至怀疑输入有问题,但看到gets也可以过,难道是思路错 ...
- 【Luogu】P1383高级打字机
可持久化线段树模板题之一. 权当温习主席树模板 #include<cstdio> #include<cstdlib> #include<cctype> #defin ...
- 学习系列 - 马拉车&扩展KMP
Manacher(马拉车)是一种求最长回文串的线性算法,复杂度O(n).网上对其介绍的资料已经挺多了的,请善用搜索引擎. 而扩展KMP说白了就是是求模式串和主串的每一个后缀的最长公共前缀[KMP更像是 ...
- 刷题总结——切蛋糕(ssoj)
题目: 切蛋糕 (cake.cpp/c/pas) [问题描述] BG 有一块细长的蛋糕,长度为�. 有一些人要来BG 家里吃蛋糕, BG把蛋糕切成了若干块(整数长度),然后分给这些人.为了公平,每个人 ...
- Longge的问题(bzoj 2705)
Description Longge的数学成绩非常好,并且他非常乐于挑战高难度的数学问题.现在问题来了:给定一个整数N,你需要求出∑gcd(i, N)(1<=i <=N). Input 一 ...
- 降雨量 BZOJ 1067
降雨量 [问题描述] 我们常常会说这样的话:“X年是自Y年以来降雨量最多的”.它的含义是X年的降雨量不超过Y年,且对于任意Y<Z<X,Z年的降雨量严格小于X年.例如2002,2003,20 ...
- (转)java 中变量存储位置总结
1.寄存器:最快的存储区, 由编译器根据需求进行分配,我们在程序中无法控制. 2. 栈:存放基本类型的变量数据和对象的引用,但对象本身不存放在栈中,而是存放在堆(new 出来的对象)或者常量池中(字符 ...
- python操作excel--生成图表
[问题] 想要折腾Python中的Excel中的图标,Chart,Graph. [解决过程] 1.参考: use python to generate graph in excel 说是可以用pywi ...
- ELK之Kibana部署、收集系统日志、一个文件收集多个日志
1.安装及配置Kibana cd /usr/local/src yum -y install kibana-5.4.0-x86_64.rpm grep "^[a-Z]" /etc/ ...