NYOJ 130相同的雪花(哈希)

相同的雪花

时间限制: 1000 ms  |  内存限制: 65535 KB
难度: 4
描述
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.
输入
The first line of the input will contain a single interger T(0<T<10),the number of the test cases.
The first line of every test case 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.
输出
For each test case,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.
样例输入
1
2
1 2 3 4 5 6
4 3 2 1 6 5
样例输出
Twin snowflakes found. 

解题思路:这个题最开始看着以为是stl方面的题,只要sort排序后找到两个相同序列就可以了,最后再按题意输出就可以了,这样借助个map映射就可以了,但仔细一看原来它们的的相对顺序是不可以改变 的,这个时候就需要找个方法来解决不改变原本序列而判断两个序列是否相同了,所以就用到了哈希来方便 这个查询,因为只要两个序列的和 相等这两个序列就有可能相等,因为有了哈希,就需要一个解决哈希冲突的方法了,用拉链法(vector)来解决哈希冲突,然后如果有两个及其以上相同的哈希值就判断这几个序列是否在顺时针或逆时针方向相同

#include <iostream>
#include <cstdio>
#include <vector>
#include <sstream>
#include <algorithm>
using namespace std;
int a[100005][7];
int Like(int x,int y)
{
    for(int i=0;i<6;i++)
    {
        if((a[x][0]==a[y][i]&&a[x][1]==a[y][(i+1)%6]&&a[x][2]==a[y][(i+2)%6]&&a[x][3]==a[y][(i+3)%6]&&a[x][4]==a[y][(i+4)%6]&&a[x][5]==a[y][(i+5)%6])///顺时针方向判断两个雪花是否相同
         ||(a[x][0]==a[y][(i+5)%6]&&a[x][1]==a[y][(i+4)%6]&&a[x][2]==a[y][(i+3)%6]&&a[x][3]==a[y][(i+2)%6]&&a[x][4]==a[y][(i+1)%6]&&a[x][5]==a[y][i]))///逆时针方向判断两个雪花是否相同
            return 1;
    }
    return 0;
}
int main()
{
    int T;scanf("%d",&T);
    while(T--)
    {
        int n,flag=0;
        vector<int> Hash[5000];//整个哈希链表的长度
        scanf("%d",&n);
        for(int j=0;j<n;j++)
        {
            int sum=0,t;
            for(int i=0;i<6;i++)
            {
                scanf("%d",&t);
                sum+=t;
                a[j][i] = t;
            }
            sum=sum%5000;///对哈希表的长度取余防止超出哈希表长度
            Hash[sum].push_back(j);
        }
        for(int i=0;i<5000;i++)
        {
            if(flag)break;///如果找到相同的雪花就没有不下去的必要了
            if(Hash[i].size()<2)continue;///只有一个或0个的不可能有重复的直接continue
            for(int j=0;j<Hash[i].size()-1;j++)
            {
                for(int k=j+1;k<Hash[i].size();k++)
                    if(Like(Hash[i][j],Hash[i][k]))///对于每个节点在找到
                    {
                        flag=1;break;
                    }
            }
        }
        if(flag)printf("Twin snowflakes found.\n");
        else printf("No two snowflakes are alike.\n");
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值