POJ3349-Snowflake snow snowflakes

Source: Internet
Author: User
Tags modulus

 

Reprinted please indicate the source: Thank you

Http://user.qzone.qq.com/289065406/blog/1304831877

General question:

Determine whether two identical snowflakes exist in n (n <100000) snowflake. Each snowflake has 6 horns and each angle has a length limit of 1000000

Conditions where two pieces of Snowflake are equal:

The length of the six snow angles is equal in order (this order can be clockwise or counterclockwise)

 

Solution:

Hash!Join Addition MethodEvaluate the key value,Link address MethodResolve Conflicts

Set the length of 6 snowflake leaves to len1 ~ Len6

Key = (len1 + len2 + len3 + len4 + len5 + len6) % prime

= (Len1% prime + len2 % prime + len3 % prime + len4 % prime + len5 % prime + len6) % prime

To avoid the occurrence of large numbers, we use the same modulus theorem to obtain the key.

 

Note: The sum of squares cannot be used for keys here. Originally, the time for this question was very compact, and the multiplication operation was a serious waste of time. Therefore, the key was used for concatenation, as long as the prime was large enough, in the same way, the address conflict can be minimized. I took the largest prime number in 10 N (that is, 100 W) as prime, prime = 999983

 

Basic practice:

From the above processing method, we can know:

Only when the two pieces of snowflake have the same key value can the two pieces of snowflake be the same.

 

When entering the K snowflake information, first obtain the key value. If the key value does not appear in hash [], store the information directly. However, if another address has been saved in hash [Key], it indicates that there have been other snowflakes with the same key value. The snowflake information is stored in hash [Key] As a linked list, thenWhile searching for storage space for the K snowflake information, it is necessary to access these snowflake with the same key one by one in the linked list, so we can address the problem at the same time, by the way, compare the information of the K snowflake and the Snowflake one by one.If K is the same as a snowflakeMark, And then wait for the subsequent input to complete, directly output the two pieces of the same Snowflake.

 

However, if no input is marked after the end, the snowflake does not exist.

 

At this time, some may have questions:

The key value can only indicate that the sum of the two snowflake leaves is equal, but it cannot indicate that the six leaves are equal, not that the six leaves are equal in order. So how can we compare the two snowflakes with the same key value?

Actually, it is okay. Assume there are two pieces of snowflake, A and B.

We fix,First compare in clockwise direction:

If a0 = b0, compare A1 and B1. ...... compare A5 and B5.

When AI appears! = Bi, turn B clockwise once,

If a0 = b1, compare A1 and b2. ...... compare A5 and B0.

.....

And so on, until B rotates five times. If the two snowflakes are not the same, the two snowflakes are clockwise.

 

Then compare the counter-clockwise direction:

It is also fixed as a. If a0 = B5, A1 and b4......... are compared in order to A5 and B0.

When AI appears! = B (5-i), B is turned counter-clockwise once,

If a0 = B4, A1 and b3...... A5 and B5 are compared in order.

.....

And so on, until B turns five times. If the two snowflakes are not the same, the two snowflakes are not in the clockwise direction.

 

For example, the worst case for comparing two pieces of snowflake is 36x2 = 72 times !!!

It can be imagined that when n =, if any two pieces are compared, the worst is 72X(10 W) ^ 2 times.

 

Two formulas are provided here:

Set I to the I-th Leaf of A and B, and J to the number of cells that B turns clockwise.

Then a (I) ---> B (I + J) % 6)

Set I to the first leaf of A and B, and J to the number of cells that B turns counter-clockwise.

Then a (I) ---> B (5-i-j + 6) % 6)

 

Therefore, in order to reduce the number of comparisons as much as possible, we need to classify the snowflake by the key value. In this case, we want Prime to be as large as possible within the appropriate range, minimize address conflicts (two or more snowflake with the same key value,The ideal condition is that their key values are equal only when the two snowflakes are the same.Then, based on the previous algorithm IDEA (only compare two pieces of snowflake with the same key value), in the ideal situation, we only need to compare once at most to get the result of "having the same snowflake". We can get the result of "having the same snowflake" at least 0 times.

 

After testing, it is found that when the prime number is about W, the key discrete degree is relatively high, there are few conflicts, and the prime is large, which has little impact on the degree of discretization and will waste space ..

When the prime number is about, there may be more than 6 K keys with the same key value. At this time, the hash advantage cannot be reflected.

 

 

// Memory time // 16696 K 3766 MS # include <iostream> using namespace STD; const _ int64 prime = 999983; // The maximum prime number in 10 n (n = 10 W in this question) class {public :__ int64 Len [6]; // The length of the 6-petal leaf} leaf [100001]; typedef class hashtable {public :__ int64 Len [6]; // records hashtable * Next, the length of the 6-petal leaf; // specifies hashtable () in case of conflict () // initial {next = 0 ;}} hashtable; hashtable * hash [prime + 1]; /* calculate the key of K snowflake */_ int64 compute_key (int K) {__ int64 key = 0; For (INT I = 0; I <6; I ++) {Key + = (leaf [K]. len [I]) % prime; key % = prime; // calculate the key using the same modulus theorem to avoid large numbers} return ++ key; // shift the key value one bit behind, set the key range from 0 ~ 999982 to 1 ~ 999983}/* determine whether two snowflakes are the same clockwise */bool clockwise (hashtable * P, int K) {for (Int J = 0; j <6; j ++) // clockwise rotate the J grid {bool flag = true; For (INT I = 0; I <6; I ++) if (leaf [K]. len [I]! = P-> Len [(I + J) % 6]) {flag = false; break;} If (FLAG) return true;} return false ;} /* determine whether two pieces of snow are the same from the clockwise direction */bool counterclockwise (hashtable * P, int K) {for (Int J = 0; j <6; j ++) // rotate the J grid {bool flag = true; For (INT I = 0; I <6; I ++) if (leaf [K]. len [I]! = P-> Len [(5-i-j + 6) % 6]) {flag = false; break;} If (FLAG) return true;} return false ;} /* Insert the K snowflake information into hashtable * // * when other snowflake information already exists in the inserted position, compare it by the way */bool insert (int K) {__ int64 key = compute_key (k); If (! Hash [Key]) {hashtable * temp = new hashtable; For (INT I = 0; I <6; I ++) temp-> Len [I] = leaf [K]. len [I]; hash [Key] = temp; // Save the address corresponding to the key} else // address conflict, open addressing, by the way, compare {hashtable * temp = hash [Key]; If (clockwise (temp, k) | counterclockwise (temp, k) // check whether the snowflake returns true; while (temp-> next) // addressing {temp = temp-> next; If (clockwise (temp, k) | counterclockwise (temp, k )) // check whether the snowflake is the same return true;} temp-> next = new hashtable; // apply for a space and save the new snowflake information for (INT I = 0; I <6; I ++) temp-> next-> Len [I] = leaf [K]. len [I];} return false;} int main (int I, Int J) {int N; // Number of snowflakes while (CIN> N) {/* Initial */memset (hash, 0, sizeof (hash); // 0 <-> null/* input */bool flag = false; // record whether the same snowflake for (I = 1; I <= N; I ++) {for (j = 0; j <6; j ++) scanf ("% i64d", & leaf [I]. len [J]);/* hash */If (! Flag) // The same snowflake flag = insert (I) does not appear yet; // if the same snowflake appears, you need to enter it later, but not processed}/* output */If (FLAG) cout <"twin snowflakes found. "<Endl; elsecout <" no two snowflakes are alike. "<Endl;} 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.