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 ...
随机推荐
- TOJ 2710: 过河 路径压缩
2710: 过河 Time Limit(Common/Java):1000MS/10000MS Memory Limit:65536KByteTotal Submit: 32 ...
- oracle客户端安装与配置
在进行开发时经常需要连接Oracle数据库,一般的场景是Oracle数据库在远程服务器上,本地计算机通过plsql developer来访问. 这就要求在本地安装好plsql developer,但是 ...
- BZOJ 2829 信用卡凸包 ——计算几何
凸包裸题 #include <map> #include <cmath> #include <queue> #include <cstdio> #inc ...
- SPOJ QTREE Query on a tree ——树链剖分 线段树
[题目分析] 垃圾vjudge又挂了. 树链剖分裸题. 垃圾spoj,交了好几次,基本没改动却过了. [代码](自带常数,是别人的2倍左右) #include <cstdio> #incl ...
- BZOJ 3907: 网格【组合数学】
Description 某城市的街道呈网格状,左下角坐标为A(0, 0),右上角坐标为B(n, m),其中n >= m.现在从A(0, 0)点出发,只能沿着街道向正右方或者正上方行走,且不能经过 ...
- FZU 2020 :组合 【lucas】
Problem Description 给出组合数C(n,m), 表示从n个元素中选出m个元素的方案数.例如C(5,2) = 10, C(4,2) = 6.可是当n,m比较大的时候,C(n,m)很大! ...
- 【2018.10.2】Note of CXM
1.有一张无向图,现在要给每个点染上黑色或白色,最后每个点的染色代价是它与离这个点最近的不同色节点的距离.求最小代价.所有边权$\geq 0$且互不相同. 分三种情况: 两点都染了色:两点都跟其它点算 ...
- android开发里跳过的坑——camera调用setDisplayOrientation设置预览显示旋转无效
问题原因,在surfaceview没有设置给camera之前调用了,所以,这个方法一定要在camera.setPreviewDisplay(surfaceHolder)这个之后,启动相机预览之前调用.
- 洛谷 [P1995] 程序自动分析
并查集+ 离散化 首先本题的数据范围很大,需要离散化, STL离散化代码: //dat是原数据,id是编号,sub是数据的副本 sort(sub + 1, sub + tot + 1); size = ...
- 洛谷 P 3371 单元最短路
题目描述 如题,给出一个有向图,请输出从某一点出发到所有点的最短路径长度. 输入输出格式 输入格式: 第一行包含三个整数N.M.S,分别表示点的个数.有向边的个数.出发点的编号. 接下来M行每行包含三 ...