Use the hash table to accelerate searching-poj 3349

Source: Internet
Author: User

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.

Each snowflake has six angular length values. For different snowflakes, these values may be reversed and the start length may not be consistent.

For example, 1 2 3 4 5 6 and 4 3 2 1 6 5, although they are different on the surface, after inversion and offset, they are the same snow.

If the input data contains the same snowflake, yes is output; otherwise, no is output.

# Include <iostream> # include <stdio. h> # include <string. h> using namespace STD; const int HN = 9999; // the size of the hash table int N; struct node {int index; node * Next ;}; int snow [100002] [6]; // storage data node nodes [100002]; // linked list node hashtable [HN]; // hash table, store the linked list header // compare whether S1 and S2 are the same bool check (INT S1, int S2) {bool flag; int temp; For (Int J = 0; j <6; j ++) {flag = true; For (INT step = 0; Step <6; Step ++) {If (snow [S1] [(j + step) % 6]! = Snow [s2] [STEP]) {flag = false; break ;}} if (FLAG) return true ;}for (Int J = 0; j <6; j ++) {flag = true; For (INT step = 0; Step <6; Step ++) {temp = J-step; If (temp <0) temp + = 6; If (snow [S1] [temp]! = Snow [s2] [STEP]) {flag = false; break ;}} if (FLAG) return true ;}return false ;} int sum; bool solve () {for (INT I = 1; I <= N; I ++) {sum = 0; For (Int J = 0; j <6; j ++) {scanf ("% d", & Snow [I] [J]); sum + = snow [I] [J];} int T = sum % HN; // use the header Insertion Method nodes [I]. index = I; nodes [I]. next = hashtable [T]. next; hashtable [T]. next = & nodes [I] ;}// compare each linked list in the hash table. A linked list has a conflicting node set for (INT I = 0; I <HN; I ++) {for (node * n1 = hashta Ble [I]. Next; N1! = NULL; n1 = N1-> next) for (node * n2 = N1-> next; N2! = NULL; N2 = n2-> next) if (check (N1-> index, n2-> index) return true;} return false;} int main () {freopen ("in.txt", "r", stdin); CIN> N; memset (hashtable, 0, sizeof (hashtable); memset (nodes, 0, sizeof (nodes); If (solve () printf ("twin snowflakes found. \ n "); elseprintf (" no two snowflakes are alike. \ n "); 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.