[ACM] poj 3349 snowflake snow snowflakes (hash search and chain resolution)

Source: Internet
Author: User
Snowflake snow snowflakes
Time limit:4000 Ms   Memory limit:65536 K
Total submissions:30512   Accepted:8024

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 shocould be flagged by your program as possibly identical.

Input

The first line of input will contain a single integerN, 0 <N≤ 100000, the number of snowflakes to follow. This will be followedNLines, 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 cocould 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 shocould print the message:
No two snowflakes are alike.
If there is a pair of possibly identical snow akes, your program shocould 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 1, 2007

 

Solution:

Snow has six angles, each of which has a length, which is given clockwise or counterclockwise. the start point is random. Two snowflakes are the same. If and only when the length of each angle is the same and must be continuous, for example, 2 3 1 4 5 6 and 2 1 3 4 5 6 won't work, 1 2 3 4 5 6 4 3 2 1 6 5 is the same (the second counter-clockwise given ). The length of the six corners of a pile of snow is given, and you can find two identical snow.

1. If two snowflakes share the same length, they have the same length and. Map each snowflake to a hash table by length. Here, the hash table uses vector <snowflake> HS []. when hs [I]. when size ()> 1, it indicates that HS [I] [J] and HS [I] [k] may be two identical snowflakes, you only need to search in HS [I.

2. How can we determine that two snowflakes A and B are the same? In two steps, 1. Fix a. Align each side of B with the first side of A, and then compare each side of A and B clockwise. 2. Fix a and align each side of B with the first side of A, and then compare each side of A and B counterclockwise.

PS: I'm sorry for the data structure I got into this question .... I didn't even know how to solve the zipper conflict during the exam ..

Code:

# Include <iostream> # include <stdio. h >#include <vector >#include <cmath> # include <algorithm> using namespace STD; const int maxn = 100002; const int prime = 99991; struct snowflake {int Len [6];} snow [maxn]; vector <snowflake> HS [prime]; void Hash (snowflake s) // ing {int ADDR = (S. len [0] + S. len [1] + S. len [2] + S. len [3] + S. len [4] + S. len [5]) % prime; HS [ADDR]. push_back (s);} bool CMP (snowflake A, snowflake B) // compare whether two snowflakes are the same {for (INT I = 0; I <6; I ++) {if (. len [0] = B. len [I] &. len [1] = B. len [(I + 1) % 6] &. len [2] = B. len [(I + 2) % 6] &. len [3] = B. len [(I + 3) % 6] &. len [4] = B. len [(I + 4) % 6] &. len [5] = B. len [(I + 5) % 6]) return true; // fixed a, clockwise comparison if (. len [0] = B. len [I] &. len [1] = B. len [(I + 5) % 6] &. len [2] = B. len [(I + 4) % 6] &. len [3] = B. len [(I + 3) % 6] &. len [4] = B. len [(I + 2) % 6] &. len [5] = B. len [(I + 1) % 6]) return true; // fixed a, counterclockwise comparison} return false;} int main () {int N; scanf ("% d", & N); snowflake SF; while (n --) {for (INT I = 0; I <6; I ++) scanf ("% d", & SF. len [I]); Hash (SF) ;}for (INT I = 0; I <prime; I ++) if (HS [I]. size ()> 1) // there may be the same snowflake {for (Int J = 0; j <HS [I]. size (); j ++) for (int K = J + 1; k <HS [I]. size (); k ++) {If (CMP (HS [I] [J], HS [I] [k]) {printf ("twin snowflakes found. "); Return 0 ;}} printf (" no two snowflakes are alike. "); Return 0 ;}


 

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.