poj 3349 Snowflake Snow Snowflakes 哈希

Snowflake Snow Snowflakes
Time Limit: 4000MS Memory Limit: 65536K
Total Submissions: 35013 Accepted: 9207

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




哈希表储存。
逆序的时候先对齐起点,然后某片雪花的第i个,对应 对齐后另一片雪花的第(5-i)个。
顺序好判断,先对齐,某片雪花的第i个,对应对齐后另一片雪花的第i个。
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<climits>
#include<queue>
#include<vector>
#include<map>
#include<sstream>
#include<set>
#include<stack>
#include<utility>
#pragma comment(linker, "/STACK:102400000,102400000")
#define PI 3.1415926535897932384626
#define eps 1e-10
#define sqr(x) ((x)*(x))
#define FOR0(i,n)  for(int i=0 ;i<(n) ;i++)
#define FOR1(i,n)  for(int i=1 ;i<=(n) ;i++)
#define FORD(i,n)  for(int i=(n) ;i>=0 ;i--)
#define  lson   num<<1,le,mid
#define rson    num<<1|1,mid+1,ri
#define MID   int mid=(le+ri)>>1
#define zero(x)((x>0? x:-x)<1e-15)
#define mp    make_pair
#define _f     first
#define _s     second

using namespace std;
const int INF =0x3f3f3f3f;
//const int maxn=    ;
//const int maxm=    ;
//const int INF=    ;
typedef long long ll;
const ll inf =1000000000000000;//1e15;
//ifstream fin("input.txt");
//ofstream fout("output.txt");
//fin.close();
//fout.close();
//freopen("a.in","r",stdin);
//freopen("a.out","w",stdout);
//by yskysker123



int n;
 int tmp[6];
const  int mod=999983;
 struct Node
 {
     int a[6];
     Node* nex;
     Node()
     {
           nex=NULL;
           for(int i=0;i<6;i++)
            a[i]=tmp[i];
    }


 };
 Node* fir[mod+10];


bool clockwise(Node *p)
{
    for(int i=0;i<6;i++)
    {

        int j;
        for( j=0;j<=5;j++)
        {
            if(tmp[j]!= p->a[ (j+i)%6  ])
                break;


        }
        if(j==6)  return true;
    }

    return false;
}

bool counterclock(Node *p)
{
    for(int i=0;i<6;i++)
    {
        int j;
        for( j=0;j<=5;j++)
        {
            if(tmp[j]!=p->a [    (5-j+i+6 )%6      ] )
                break;
        }
        if(j==6)  return true;
    }
    return false;
}

bool insert(int sum)
{
    if(fir[sum]==NULL)
    {
        fir[sum]=new Node;
        return  false;
    }
    Node*  p;
    for(  p=fir[sum];p ;p=p->nex)
    {
       if( clockwise(p) )  return true;
       if( counterclock(p) )  return true;


    }
    p=new Node;

    return false;


}


 void dele(Node* p)
 {
     if(p==NULL)  return;
     dele ( p->nex       );
     delete p;
 }
int main()
{

   while(~ scanf("%d",&n)  )
   {
       memset(fir,0,sizeof fir);
    bool ok=0;
    int sum;
    FOR1(i,n)
    {
        sum=0;
        FOR0(j,6)
        {

            scanf("%d",&tmp[j]);
            sum+=tmp[j]%mod;

        }
        if(ok)  continue;
        sum%=mod;
       if( insert(sum) )   ok=1;

    }
      if(ok)  puts("Twin snowflakes found.");
     else puts("No two snowflakes are alike.");


     for(int i=1;i<=mod;i++)
     {
         dele(fir[i]);
     }
}

    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值