Poj3349:snowflake Snow Snowflakes (hash table)

Source: Internet
Author: User

Study the hash Table:

Each snowflake has its own 6 arm values, and if two snowflakes start at the same or a different position clockwise or counterclockwise, then the two snowflakes are equal.

We use the hash method so that each query time is O (1) and the total is O (n).

The essence of the hash is to map the value to the address or subscript, if the different key values correspond to the same indice, you need to do chaining processing, bar Indice point to a linked list, each node of the linked list stores a different key value that shares the same indice.

So the core of the problem becomes: Bar equivalent snowflakes mapped to the same indice. Here snowflake is a special type, we can not directly map, so we put each piece of snowflake 6 arm value and modulus as the Snowflake key value.

namely: Key= (Arm[0]+arm[1]+arm[2]+arm[3]+arm[4]+arm[5]);

and Indice=key%prime

Because the number of snowflakes on the topic of the largest range is 10^6. So we take a 99983, which is very close to 10^6, as Prime (PS: In fact, the large prime numbers around 10^6 can be chosen randomly).

But here's another question: even if the key value is the same, it doesn't mean that two snowflakes are the same, so when we map all the snowflakes to the hash table, the snowflakes stored in the adjacent table that each indice points to are just arm and the same.

-----------------------------------------------------------------------------------------

OK, now we're looking for the same snowflake:

sequentially, each snowflake is enumerated, calculates his indice value, and then looks at whether the adjacency table pointed to by Indice is empty, and if it is empty, then there is no record of "same" snowflakes. If it is not empty, then there is arm and the same snowflake as the current snowflake in the hash table, so we further look in the adjacency table that Indice points to. If you find a snowflake with arm sequential "match", output "Twin snowflakes found." And then the program ends. If the adjacency table does not have the "equal" snowflakes we want, then we enumerate the next snowflake.

If the enumeration plays all snowflakes without finding a snowflake equal to it, the output " No. Snowflakes is alike. ", the program ends.

#include<iostream>#include<algorithm>#include<cstdio>#include<cstring>#include<map>#include<vector>UsingNamespace Std;Constint max_size=100010;Constint prime=99991; vector<Int> HSH[Prime];int arm[Max_size][6];int n;boolIsthesame(int a,int b){For(int I=0; I<6; I++){If((Clockwisearm[A][0]==arm[b][i]&& Arm[A][1]==arm[b[(I+1)%6]&& Arm[A][2]==arm[b[(I+2)%6]&& Arm[A][3]==arm[b[(I+3)%6]&& Arm[A][4]==arm[b[(I+4)%6]&& Arm[A][5]==arm[b[(I+5)%6])||Counter Clockwise(Arm[A][0]==arm[b][i]&& Arm[A][1]==arm[b[(I+5)%6]&& Arm[A][2]==arm[b[(I+4)%6]&& Arm[A][3]==arm[b[(I+3)%6]&& Arm[A][4]==arm[b[(I+2)%6]&& Arm[A][5]==arm[b[(I+1)%6]))ReturnTrue;}ReturnFalse;}IntMain(){scanf("%d", &n);LongLong sum, key;For(int I=0; I<n; I++){For(Int J=0; j<6; j++){scanf("%d", &arm[I][j]);}}For(int I=0; I<n; I++){Sum=0;For(Int J=0; j<6; j++){Sum+=arm[I][j]*2;}key=sum%prime; vector<Int>::size_type K;For(k=0; k[Key].Size(); k++){If(Isthesame(I, HSH[Key][k{printf (  "Twin Snowflakes found. \n " Exit0} Span class= "Sh_cbracket" >}hsh[key]. Push_back} printf ( "No." Snowflakes is alike.< Span class= "Sh_specialchar" >\n "}   

Poj3349:snowflake Snow Snowflakes (hash table)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.