POJ 3349 ——Snowflake Snow Snowflakes 链地址法哈希

来源:互联网 发布:telnet 端口号 结果 编辑:程序博客网 时间:2024/06/04 01:34
Snowflake Snow Snowflakes
Time Limit: 4000MS
Memory Limit: 65536KTotal Submissions: 32269
Accepted: 8497

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

21 2 3 4 5 64 3 2 1 6 5

Sample Output

Twin snowflakes found.

Source

CCC 2007



链地址法哈希,先对输入的数从小到大排序,然后用最小的数对100000取余,遍历以最小值开头的哈希表,找是否完全匹配。。。。。

#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;struct node{    int data;    node *next;}head[100010];int mp[100010][6];int main(){    int n,js;    bool flag = false;    memset(head,0,sizeof(head));    scanf("%d",&n);    for(int i = 0;i < n;i++)    {        for(int j = 0;j < 6;j++)        {            scanf("%d",&mp[i][j]);        }        if(flag)            continue;        sort(mp[i],mp[i] + 6);        int zh = mp[i][0] % 100000;        node *p = (&head[zh])->next;        node *q = &(head[zh]);        while(p)        {            js = 0;            for(int j = 0;j < 6;j++)            {                if(mp[p->data][j] == mp[i][j])                    js++;                else                    break;            }            if(js == 6)            {                flag = true;                break;            }            q = p;            p = p->next;        }        if(!p)        {            node *r = new node;            r->data = i;            r->next = NULL;            q->next = r;        }    }    if(flag)        printf("Twin snowflakes found.\n");    else        printf("No two snowflakes are alike.\n");    return 0;}


0 0
原创粉丝点击