• poj3349 Snowflake Snow Snowflakes【HASH】


    Snowflake Snow Snowflakes
    Time Limit: 4000MS   Memory Limit: 65536K
    Total Submissions: 49991   Accepted: 13040

    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.

    Source

    题意:

    一朵雪花有六条边,给n朵雪花,问其中有没有相同的雪花。当两个雪花的边都对应相同时,雪花是相同的。

    思路:

    ,其中P是我们选定的一个较大的质数

    这个HASH函数把所有的雪花分成了P类,对于每一朵 雪花,定位到head[]这个表头所指向的链表。如果该链表中不包含和这朵雪花相同的雪花,就在表头后插入一个新节点(对于一些题目可以在该节点上记录出现了的次数)。

     1 #include <iostream>
     2 #include <set>
     3 #include <cmath>
     4 #include <stdio.h>
     5 #include <cstring>
     6 #include <algorithm>
     7 #include <map>
     8 using namespace std;
     9 typedef long long LL;
    10 #define inf 0x7f7f7f7f
    11 
    12 const int maxn = 1e6 + 5;
    13 int snow[maxn][6], head[maxn], nxt[maxn];
    14 int n, tot, P = 99991;
    15 
    16 int H(int *a)
    17 {
    18     int sum = 0, mul = 1;
    19     for(int i = 0; i < 6; i++){
    20         sum = (sum + a[i]) % P;
    21         mul = (long long)mul * a[i] % P;
    22     }
    23     return (sum + mul) % P;
    24 }
    25 
    26 bool eequal(int *a, int *b)
    27 {
    28     for(int i = 0; i < 6; i++){
    29         for(int j = 0; j < 6; j++){
    30             bool eq = 1;
    31             for(int k = 0; k < 6; k++){
    32                 //cout<<a[(i + k) % 6]<<" "<<b[(i + k) % 6]<<endl;
    33                 if(a[(i + k) % 6] != b[(j + k) % 6]) eq = 0;
    34             }
    35             if(eq) return 1;
    36             eq = 1;
    37             for(int k = 0; k < 6; k++){
    38                 if(a[(i + k) % 6] != b[(j - k + 6) % 6])eq = 0;
    39             }
    40             if(eq)return 1;
    41         }
    42     }
    43     return 0;
    44 }
    45 
    46 bool insertt(int *a)
    47 {
    48     int val = H(a);
    49     //cout<<val<<endl;
    50     for(int i = head[val]; i; i = nxt[i]){
    51         //cout<<2<<endl;
    52         if(eequal(snow[i], a)){
    53             //cout<<1<<endl;
    54             return 1;
    55         }
    56     }
    57     ++tot;
    58     memcpy(snow[tot], a, 6 * sizeof(int));
    59     nxt[tot] = head[val];
    60     head[val] = tot;
    61     return 0;
    62 }
    63 
    64 int main()
    65 {
    66     //freopen("in.txt", "r", stdin);
    67     //freopen("out.txt", "w", stdout);
    68     scanf("%d", &n);
    69     //tot = 0;
    70     //memset(head, 0, sizeof(head));
    71     for(int i = 1; i <= n; i++){
    72         int a[10];
    73         for(int j = 0; j < 6; j++){
    74             scanf("%d", &a[j]);
    75         }
    76         if(insertt(a)){
    77             printf("Twin snowflakes found.
    ");
    78             return 0;
    79         }
    80     }
    81     printf("No two snowflakes are alike.
    ");
    82 
    83 
    84 }
  • 相关阅读:
    mysql 修改表
    mac下安装MySQL 5.7
    win&linux下path中%%与$ 以及;与:区别,
    PATH
    转 path设置方式
    MyEclipse乱码问题
    03 最大的数据库 information_schema介绍以及sql注入第一题题解
    02.mysql数据库 基本命令
    01.Windows进入MySQL数据库
    Web web4
  • 原文地址:https://www.cnblogs.com/wyboooo/p/9811901.html
Copyright © 2020-2023  润新知