zoukankan      html  css  js  c++  java
  • 相同的雪花 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.
    ");
            else
                printf("No two snowflakes are alike.
    ");
            Free(H);
        }
        return 0;
    }
  • 相关阅读:
    洛谷 P1567 统计天数【最长上升子序列/断则归一】
    洛谷 P3742 umi的函数【构造】
    洛谷 P1036 选数【背包型DFS/选or不选】
    nyoj zb的生日【背包型DFS/选or不选】
    POJ 3628 Bookshelf 2【背包型DFS/选or不选】
    【AHOI2013复仇】从一道题来看DFS及其优化的一般步骤和数组分层问题【转】
    洛谷 P1217 [USACO1.5]回文质数 Prime Palindromes【取回文数/数论/字符串】
    洛谷 P1004 方格取数 【多线程DP/四维DP/】
    Codeforces Round #449 (Div. 2) B. Chtholly's request【偶数位回文数】
    Codeforces Round #449 (Div. 2) A. Scarborough Fair【多次区间修改字符串】
  • 原文地址:https://www.cnblogs.com/joeylee97/p/6882328.html
Copyright © 2011-2022 走看看