您的位置:首页 > 其它

ACM: poj 3349 (学习hash多种方法.…

2016-05-19 23:17 387 查看
                                                       
Snowflake Snow Snowflakes

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.

 

题意: 找两片完全相同的雪花,雪花有6个边. 在n个雪花中判断是否有两个相同的雪花.

 

解题思路:

                
1. 看了讨论里面的题解. 我采取最直接的方法(排序).

                
2. 还有许多用hash方法.学习学习:

      
1. 直接相加, 把(总和%大质数)为key.

      
2. 平方和相加, 把(总和%大质数)为key.

      
3. 从小到大的顺序, 对v[i]<<(i*3)依次异或,
然后模一个大质数作为key.(by hust07p43)

      
4. 六个数中非零数的积再乘上一个大质数,然后模一个100w上下的数。
自己拿随机数据测下来110w左右的效果最好,随机情况下数据量是10w的时候hash值相同的情况只有6k多个,几乎可以忽略。(by
killertom)

      
5. 依次对每个数异或所得的数作为key. (by archerstarlee) 6. (a[0] + a[2] +
a[4])&(a[1] + a[3] + a[5]), 再模一个大质数.
中间的&还可以改成'|' 或者'^'.非常巧妙! (只对本题适用的hash方法)

       
其实最关键的就是要开放式寻址解决hash冲突, 不要以为hash就能解决问题了.

       
最后就是用getchar和gets来进行输入转换更为快速. G++比GCC快一些.

 

代码:

 

#include <cstdio>

#include <cstdlib>

#include <iostream>

#include <algorithm>

using namespace std;

#define MAX 100005

struct node

{

    int a, b, c,
d, e, f;

}flake[MAX];

int n;

int cmp1(const void *a,const void *b)

{

    return
*(int*)a - *(int*)b;

}

int cmp2(const void *a,const void *b)

{

    
struct node *aa=(node *)a;

    
struct node *bb=(node *)b;

    
if(aa->a!=bb->a)

  
   
  return
(aa->a)-(bb->a);

     else
if(aa->a==bb->a&&aa->b!=bb->b)

  
   
  return
(aa->b)-(bb->b);

     else
if(aa->a==bb->a&&aa->b==bb->b&&aa->c!=bb->c)

  
   
  return
(aa->c)-(bb->c);

     else
if(aa->a==bb->a&&aa->b==bb->b&&aa->c==bb->c&&aa->d!=bb->d)

  
   
  return
(aa->d)-(bb->d);

     else
if(aa->a==bb->a&&aa->b==bb->b&&aa->c==bb->c&&aa->d==bb->d&&aa->e!=bb->e)

  
   
  return
(aa->e)-(bb->e);

     else

  
   
  return
(aa->f)-(bb->f);

}

int main()

{

//  
 freopen("input.txt","r",stdin);

  
 while(scanf("%d",&n) !=
EOF)

    {

  
   
 for(int i = 0; i < n; ++i)

  
   
 {

  
   
   
 scanf("%d %d %d %d %d
%d",&flake[i].a,&flake[i].b,&flake[i].c,&flake[i].d,&flake[i].e,&flake[i].f);

  
   
   
 qsort(flake+i,6,sizeof(int),cmp1);

  
   
 }

  
   
 qsort(flake,n,sizeof(flake[0]),cmp2);

  
   
 

  
   
 int flag = 0;

  
   
 for(int i = 0; i < n-1; ++i)

  
   
 {

  
   
   
 if(flake[i].a == flake[i+1].a
&& flake[i].b == flake[i+1].b
&& flake[i].c == flake[i+1].c
&& flake[i].d == flake[i+1].d
&& flake[i].e == flake[i+1].e
&& flake[i].f == flake[i+1].f
)

  
   
   
 {

  
   
   
   
 printf("Twin snowflakes found.\n");

  
   
   
   
 flag = 1;

  
   
   
   
 break;

  
   
   
 }

  
   
 }

  
   
 if(flag == 0)

  
   
 {

  
   
   
 printf("No two snowflakes are alike.\n");

  
   
 }

    }

    

    return
0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: