Snowflake Snow Snowflakes
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

题意:
判断给出的n朵雪花中有没有两朵完全相同(对应的边长度相同,位置相同,顺序可正可逆)
 
先上第一次WA的代码:
#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)的更多相关文章

  1. 题目1022:游船出租(hash简单应用)

    问题来源 http://ac.jobdu.com/problem.php?pid=1022 问题描述 每次输入:船号(1~100) 键值(S或E) 发生时间(小时:分钟).当船号为0时,代表一天结束: ...

  2. sdoi2013 spring(hash+容斥)

    大体思路是先求出来\(f[i]\)代表有至少\(i\)个位置相同的点对数. 然后就已经没什么好害怕的了(跟BZOJ3622一样) 然后这个\(f[i\)]怎么求呢? 最无脑的方法就是枚举位置,然后\( ...

  3. 字符串学习总结(Hash & Manacher & KMP)

    前言 终于开始学习新的东西了,总结一下字符串的一些知识. NO.1 字符串哈希(Hash) 定义 即将一个字符串转化成一个整数,并保证字符串不同,得到的哈希值不同,这样就可以用来判断一个该字串是否重复 ...

  4. HDU - 3973 AC's String(Hash+线段树)

    http://acm.hdu.edu.cn/showproblem.php?pid=3973 题意 给一个词典和一个主串.有两种操作,查询主串某个区间,问这主串区间中包含多少词典中的词语.修改主串某一 ...

  5. 牛客训练三:处女座的比赛(hash打表)

    题目链接:传送门 思路:由于MOD只有9983大小,所以四位小写字母的字符串组合有26+26^2+26^3+26^4=475254种组合. 所以只要每次枚举出从1到475254中的hash值对应的字符 ...

  6. 哈希与位图(Hash and BitMap)

    Hash:哈希机制 BitMap:位图机制 目的:都是为了保证检索方便而设置的数据结构 对于大数据进行排序,由于内存限制,不可能在内存中进行,所以采取BitMap机制 为了在大数据中快速检索以及操作数 ...

  7. 7-14 电话聊天狂人(25 分)(Hash表基本操作)

    7-14 电话聊天狂人(25 分) 给定大量手机用户通话记录,找出其中通话次数最多的聊天狂人. 输入格式: 输入首先给出正整数N(≤10​5​​),为通话记录条数.随后N行,每行给出一条通话记录.简单 ...

  8. bzoj2124 等差子序列(hash+线段树)

    2124: 等差子序列 Time Limit: 3 Sec  Memory Limit: 259 MBSubmit: 719  Solved: 261[Submit][Status][Discuss] ...

  9. nyoj--138--找球号(二)(hash+邻接表)

    找球号(二) 时间限制:1000 ms  |  内存限制:65535 KB 难度:5 描述 在某一国度里流行着一种游戏.游戏规则为:现有一堆球中,每个球上都有一个整数编号i(0<=i<=1 ...

随机推荐

  1. Playrix Codescapes Cup (Codeforces Round #413, rated, Div. 1 + Div. 2) 一夜回到小学生

    我从来没想过自己可以被支配的这么惨,大神讲这个场不容易掉分的啊 A. Carrot Cakes time limit per test 1 second memory limit per test 2 ...

  2. RESTful API接口

    我所理解的RESTful Web API [设计篇] 百度:RESTful restful一种软件架构风格.设计风格,而不是标准,只是提供了一组设计原则和约束条件.它主要用于客户端和服务器交互类的软件 ...

  3. JS事件兼容性

    事件代理的时候,使用事件对象中的srcElement属性,获取触发元素.IE浏览器支持window.event.srcElement , 而firefox支持window.event.target. ...

  4. Luogu【P2904】跨河(DP)

    题目链接在这里 此题DP.用一个前缀和一样的东西,把载i个奶牛的时间求出来,然后DP代码如下: ;i<=n;++i){ f[i]=que[i]; ;j<i;++j) f[i]=min(f[ ...

  5. BZOJ 2337 [HNOI2011]XOR和路径 ——期望DP

    首先可以各位分开求和 定义$f(i)$表示从i到n的期望值,然后经过一些常识,发现$f(n)=1$的时候的转移,然后直接转移,也可以找到$f(n)=0$的转移. 然后高斯消元31次就可以了. #inc ...

  6. P1282 多米诺骨牌 (差值DP+背包)

    题目描述 多米诺骨牌有上下2个方块组成,每个方块中有1~6个点.现有排成行的 上方块中点数之和记为S1,下方块中点数之和记为S2,它们的差为|S1-S2|.例如在图8-1中,S1=6+1+1+1=9, ...

  7. 【leetcode dp】629. K Inverse Pairs Array

    https://leetcode.com/problems/k-inverse-pairs-array/description/ [题意] 给定n和k,求正好有k个逆序对的长度为n的序列有多少个,0& ...

  8. SPOJ 4060 A game with probability

    博弈论+dp+概率 提交链接- 题意不是很好懂 Ai 表示剩 i 个石头. A 先手的获胜概率. Bi 表示剩 i 个石头. B先手的获胜概率. 如果想选,对于 Ai: 有 p 的概率进入 Bi−1 ...

  9. 16.1114 模拟考试T1

    1.正确答案 [题目描述] 小H与小Y刚刚参加完UOIP外卡组的初赛,就迫不及待的跑出考场对答案. “吔,我的答案和你都不一样!”,小Y说道,”我们去找神犇们问答案吧”. 外卡组试卷中共有m道判断题, ...

  10. 【HDOJ6222】Heron and His Triangle(Java,二分,递推)

    题意:让你找这样的一个三角形,三条边为t,t-1,t+1,并且面积为整数,最后满足t大于等于n. n<=1e30 思路:直接推式子不会,打表找规律 f(n)=4*f(n-1)-f(n-2)(n& ...