相同的雪花 Hash

相同的雪花

时间限制: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.

注意hash表大小
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<sstream>
#include<algorithm>
#include<queue>
#include<deque>
#include<iomanip>
#include<vector>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<fstream>
#include<memory>
#include<list>
#include<string>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define MAXN 100001
#define INF 1000000009
#define eps 0.00000001
/*
寻找是否有两片相同的雪花
雪花的边可能从任意一边 开始!
Hash 雪花总边长
*/
typedef struct Hashnode
{
    int a[6];
    struct Hashnode* next;
}*List;
typedef struct HashT
{
    List* L;
}*HashTable;
int NextPrime(int n)
{
    int i;
    for (int tmp = n;; tmp++)
    {
        for (i = 2; i*i <= tmp; i++)
        {
            if (tmp%i == 0)
                break;
        }
        if (i*i > tmp)
            return i;
    }
}
HashTable Init(int n)
{
    HashTable H = (HashTable)malloc(sizeof(HashT));
    H->L = (List*)malloc(sizeof(List)*MAXN);
    for (int i = 0; i < MAXN; i++)
    {
        H->L[i] = (List)malloc(sizeof(Hashnode));
        memset(H->L[i]->a, 0, sizeof(H->L[i]->a));
        H->L[i]->next = NULL;
    }
    return H;
}
void Free(HashTable H)
{
    for (int i = 0; i < MAXN; i++)
        free(H->L[i]);
    free(H->L);
    free(H);
}
int Hash(int sum, int h)
{
    return sum%h;
}
List Find(int a[6], int sum, HashTable H)
{
    List p = H->L[Hash(sum, MAXN)];
    List l = p->next;
    int i;
    while (l != NULL)
    {
        for (int j = 0; j < 6; j++)
        {
            if (l->a[j] == a[0])
            {
                for (i = 1; i < 6; i++)
                {
                    if (l->a[(j + i) % 6] != a[i])
                        break;
                }
                if (i == 6) return l;
                for (i = 1; i < 6; i++)
                {
                    if (l->a[(j - i + 6) % 6] != a[i])
                        break;
                }
                if (i == 6) return l;
            }
        }
        l = l->next;
    }
    return NULL;
}
bool Insert(int a[6], int sum, HashTable H)
{
    List L = H->L[Hash(sum, MAXN)];
    List p = Find(a, sum, H);
    if (p == NULL)
    {
        List tmp = (List)malloc(sizeof(Hashnode));
        for (int i = 0; i < 6; i++)
            tmp->a[i] = a[i];
        tmp->next = L->next;
        L->next = tmp;
        return true;
    }
    return false;
}
int main()
{
    int T, n, tmp[6];
    bool f;
    scanf("%d", &T);
    while (T--)
    {
        scanf("%d", &n);
        f = false;
        HashTable H = Init(n);
        for (int i = 0; i < n; i++)
        {
            int sum = 0;
            for (int j = 0; j < 6; j++)
            {
                scanf("%d", &tmp[j]);
                sum += tmp[j];
            }
            if (!f && !Insert(tmp, sum, H))
                f = true;
        }
        if (f)
            printf("Twin snowflakes found.\n");
        else
            printf("No two snowflakes are alike.\n");
        Free(H);
    }
    return 0;
}

时间: 2024-11-06 07:31:14

相同的雪花 Hash的相关文章

POJ 3349 HASH

题目链接:http://poj.org/problem?id=3349 题意:你可能听说话世界上没有两片相同的雪花,我们定义一个雪花有6个瓣,如果存在有2个雪花相同[雪花是环形的,所以相同可以是旋转过后相同]则输出“Twin snowflakes found.”,否则输出“No two snowflakes are alike.”. 思路:最简单的就是两两判断,但是这样的复杂度为O(n^2),TLE.所以我们要尽量减少判断次数,我们用把拥有6个瓣的雪花HASH成一个数字,只有两个雪花用有相同HA

POJ 3349 Snowflake Snow Snowflakes(哈希表)(转)

题意:判断有没有两朵相同的雪花.每朵雪花有六瓣,比较花瓣长度的方法看是否是一样的,如果对应的arms有相同的长度说明是一样的.给出n朵,只要有两朵是一样的就输出有Twin snowflakes found.,如果任何两个都是不一样的输出No two snowflakes are alike.n=100,000. 思路:最 简单的就是枚举每两片雪花,判断他们是否相同.时间复杂度为O(n*n),显然效果不理想.有没有更好的算法呢?hash:每读进一片雪花,将雪花 hash,判断hash表里是否有相同

【hash表】收集雪花

[哈希和哈希表]收集雪花 题目描述 不同的雪花往往有不同的形状.在北方的同学想将雪花收集起来,作为礼物送给在南方的同学们.一共有n个时刻,给出每个时刻下落雪花的形状,用不同的整数表示不同的形状.在收集的过程中,同学们不希望有重复的雪花.你可以从任意a时刻开始,在b时刻停止.a到b时刻中间的雪花也都将被收集.他们希望收集的雪花最多. 输入 第一行一个正整数n:第2行到第n+1行表示n个时刻雪花的形状. 输出 最多能收集雪花的数量. 样例输入 5 1 2 3 2 1 样例输出 3 提示 n≤1e6,

NYOJ130 相同的雪花 【Hash】

相同的雪花 时间限制: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 searc

NYOJ130 同样的雪花 【Hash】

同样的雪花 时间限制: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 sea

[poj3349]Snowflake Snow Snowflakes(hash)

Snowflake Snow Snowflakes Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 37615 Accepted: 9882 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

hash应用以及vector的使用简介:POJ 3349 Snowflake Snow Snowflakes

今天学的hash.说实话还没怎么搞懂,明天有时间把知识点总结写了,今天就小小的写个结题报告吧! 题意: 在n (n<100000)个雪花中判断是否存在两片完全相同的雪花,每片雪花有6个角,每个角的长度限制为1000000 两片雪花相等的条件: 雪花6个角的长度按顺序相等(这个顺序即可以是顺时针的也可以是逆时针的) 解题思路: hash:连加求余法 求key 值,链地址法解决冲突,连加求余法 求key 值挺简单,关于链地址法解决冲突可以通过c++中,vector容器可以较为方便的实现. 下面先介绍

poj 3349 数组的hash(最常用、最普通的哈希表建立)

http://poj.org/problem?id=3349 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

POJ 3349:Snowflake Snow Snowflakes(数的Hash)

http://poj.org/problem?id=3349 Snowflake Snow Snowflakes Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 37609   Accepted: 9878 Description You may have heard that no two snowflakes are alike. Your task is to write a program to determine