• Snowflake Snow Snowflakes_哈希表


    Time Limit: 4000MS   Memory Limit: 65536K
    Total Submissions: 38685   Accepted: 10142

    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.

    每个雪花都有六个分支,用六个整数代表,这六个整数是从任意一个分支开始,朝顺时针或逆时针方向遍历得到的。输入多个雪花,判断是否有形状一致的雪花存在。

    简单的数字哈希,要注意的是每种雪花可以由多种数字组合表示。

    比如输入的是1 2 3 4 5 6,

    则2 3 4 5 6 1,3 4  5 6 1 2,……,6 5 4 3 2 1,5 4 3 2 1 6等都是相同形状的。

    因此可以在读入一个雪花的时候把这些情况全部放入哈希表中,如果某次插入的时候发生冲突,则说明存在重复的雪花,并且后面的不需要再处理。

    #include<iostream>
    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    using namespace std;
    const int N=1300005;
    const int M=1300007;
    struct node
    {
        int num[6];
        int next;
    };
    int cur;
    node a[N];
    int hashtb[M];
    void init()
    {
        cur=0;
        for(int i=0;i<M;i++) hashtb[i]=-1;
    }
    unsigned int gethash(int *num)//哈希存储
    {
        unsigned int sum=0;
        for(int i=0;i<6;i++)
        {
            sum+=num[i];
        }
        return sum%M;
    }
    bool cmp(int *num1,int *num2)
    {
        for(int i=0;i<6;i++)
        {
            if(num1[i]!=num2[i]) return false;
        }
        return true;
    }
    void insert(int *num,unsigned int pos)
    {
        for(int i=0;i<6;i++) a[cur].num[i]=num[i];
        a[cur].next=hashtb[pos];
        hashtb[pos]=cur;
        cur++;
    }
    bool search(int *num)
    {
        unsigned pos=gethash(num);//边长之和相等的雪花,pos是一样的;
        int pp=hashtb[pos];
        while(pp!=-1)
        {
            if(cmp(num,a[pp].num)) return true;//比较是否一致
            pp=a[pp].next;
        }
        insert(num,pos);
        return false;
    }
    int main()
    {
        int n;
        bool flag=false;
        int num[2][12];//num[2]是表示一个顺时针顺序,一个逆时针顺序,【12】0-5存放六条边,再复制到6-11,这样的话,在i遍历0-5时【i,i+6】是全部排列顺序
        init();
        scanf("%d",&n);
        while(n--)
        {
            for(int i=0;i<6;i++)
            {
                scanf("%d",&num[0][i]);
                num[0][i+6]=num[0][i];
            }
            if(flag) continue;
            for(int j=0;j<6;j++)//顺,逆时针转换
            {
                num[1][j]=num[0][5-j];
                num[1][j+6]=num[1][j];
            }
            for(int i=0;i<6;i++)
            {
                if(search(num[0]+i)||search(num[1]+i))//只要在顺,逆时针两种情况下一种存在两个相同雪花,即为真;
                {
                    flag=true;
                    break;
                }
            }
        }
        if(flag) printf("Twin snowflakes found.
    ");
        else printf("No two snowflakes are alike.
    ");
        return 0;
    }
    #include<iostream>
    #include<string.h>
    #include<stdio.h>
    using namespace std;
    const int N=1400005;
    const int prime=1400005;
    struct node
    {
        int num[6];
    }a[N];
    struct hashtb
    {
        int num[6];
        hashtb *next;
        hashtb()
        {
            next=0;
        }
    };
    hashtb *ha[prime];
    bool flag;
    int getkey(int k)
    {
        int key=0;
        for(int i=0;i<6;i++)
        {
            key+=a[k].num[i]%prime;
            key%=prime;
        }
        return key+1;
    }
    bool cmp(hashtb *tmp,int k)
    {
        int num[12];
        for(int i=0;i<6;i++)
        {
            num[i]=a[k].num[i];
            num[i+6]=num[i];
        }
        bool ok=false;
        for(int i=0;i<6;i++)
        {
            int j;
            for(j=0;j<6;j++)
            {
                if(tmp->num[j]!=num[i+j]) break;
            }
            if(j==6) ok=true;
        }
        if(ok) return true;
        else return false;
        
    }
    bool incmp(hashtb *tmp,int k)
    {
        int num[12];
        for(int i=0;i<6;i++)
        {
            num[i]=a[k].num[5-i];
            num[i+6]=num[i];
        }
        bool ok=false;
        for(int i=0;i<6;i++)
        {
            int j;
            for(j=0;j<6;j++)
            {
                if(tmp->num[j]!=num[i+j]) break;
            }
            if(j==6) ok=true;
        }
        if(ok) return true;
        else return false;
        
    }
    bool insert(int k)
    {
        int key=getkey(k);
        if(!ha[key])
        {
            hashtb *tmp=new hashtb;
            for(int i=0;i<6;i++)
            {
                tmp->num[i]=a[k].num[i];
            }
            ha[key]=tmp;
        }
        else
        {
            hashtb *tmp=ha[key];
            if(cmp(tmp,k)||incmp(tmp,k))
                return true;
            while(tmp->next)
            {
                tmp=tmp->next;
                if(cmp(tmp,k)||incmp(tmp,k))
                    return true;
            }
            tmp->next=new hashtb;
            for(int i=0;i<6;i++)
            {
                tmp->next->num[i]=a[k].num[i];
            }
        }
        return false;
    }
    int main()
    {
        int n;
        while(~scanf("%d",&n))
        {
            flag=false;
            memset(ha,0,sizeof(ha));
            for(int j=1;j<=n;j++)
            {
                for(int i=0;i<6;i++)
                {
                    scanf("%d",&a[j].num[i]);
                }
                if(!flag)
                {
                    if(insert(j)) flag=true;
                }
            }
            if(flag)
                cout<<"Twin snowflakes found."<<endl;
            else
                cout<<"No two snowflakes are alike."<<endl;
        }
        return 0;
    }
  • 相关阅读:
    Bean
    DI
    require import export
    JSON转js
    vue路由相关
    JS引号区别
    Go语言系列之标准库strconv
    Go语言系列之标准库flag
    Go语言系列之并发编程
    Go语言系列之自定义实现日志库
  • 原文地址:https://www.cnblogs.com/iwantstrong/p/5789666.html
Copyright © 2020-2023  润新知