Snowflake snow snowflakes
Time limit:4000 Ms |
|
Memory limit:65536 K |
Total submissions:30529 |
|
Accepted:8033 |
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
Hash Table search. If you have any questions about the first hash table, please contact us.
Question: In n (n <100000) snowflakes, determine whether two completely identical snowflakes exist. Each snowflake has six angles, and the length of each angle is limited to 1000000. Conditions where two snowflakes are equal: the length of the six angles of the snowflake is equal in order (this order can be clockwise or counterclockwise)
Ideas:The general idea is to use
Sum remainder MethodTo obtain the key value of the snowflake.
ZipperResolve the conflict. At the same time, if a conflict is detected, it is determined in turn that the snowflake with the same key value is the petal length.
SequenceAre they the same? If they are the same, two identical snowflakes are found. Exit the loop and output the result. For details, see the blog of the great God: POJ3349-Snowflake snow snowflakes said in detail.
Note:: The program is terminated before all input data is input. For example, if the input is not complete, the same snowflake will be detected. In this case, the break can jump out of the loop and stop the input without affecting the evaluation result. It can save a lot of time.
Code:
1 # include <iostream> 2 # include <stdio. h> 3 4 using namespace STD; 5 6 # define maxprime 100001 7 8 struct hashnode {9 int Len [6]; 10 hashnode * Next; 11 }; 12 hashnode hashtable [maxprime] = {0}; // hash table. Solve hash conflicts by zipping 13 14 int getkey (hashnode * P) // hash function and sum the remainder method. Calculate the key value of a snowflake. The value is 15 {16 int I, key = 0; 17 for (I = 0; I <6; I ++) 18 Key = (Key + P-> Len [I]) % maxprime; 19 Return key; 20} 21 22 bool clockwise (hashnode * P, hashnode * q) // clockwise check whether there are any 23 {24 int I, j; 25 for (I = 0; I <6; I ++) {26 for (j = 0; j <6; j ++) 27 if (p-> Len [J]! = Q-> Len [(I + J) % 6]) 28 break; 29 If (j> = 6) 30 break; 31} 32 // find two identical snowflake 33 if (I <6) 34 return true; 35 else 36 return false; 37} 38 39 bool counterclockwise (hashnode * P, hashnode * q) // check whether there is any same order of 40 {41 int I, j; 42 for (I = 0; I <6; I ++) {43 for (j = 0; j <6; j ++) 44 If (p-> Len [J]! = Q-> Len [(I + 6-j) % 6]) 45 break; 46 If (j> = 6) 47 break; 48} 49 // find two identical snowflake 50 if (I <6) 51 return true; 52 else 53 return false; 54} 55 56 bool iscom (hashnode * P) // determine whether the snowflake is exactly the same as the previous snowflake. 57 {58 int key = getkey (p); 59 If (hashtable [Key]. next = NULL) {// no conflict 60 hashtable [Key]. next = P; 61 Return false; 62} 63 else {// conflict generated 64 // The zipper method resolves the conflict 65 hashnode * q = & hashtable [Key]; 66 p-> next = Q-> next; 67 Q-> next = P; 68 69 q = p-> next; // compare 70 while (q) {71 If (clockwise (p, q) | counterclockwise (p, q) // There is a sequence in the Same Direction clockwise or counterclockwise, indicating that the same snowflake 72 return true; 73 q = Q-> next; 74} 75 return false; 76} 77} 78 79 int main () 80 {81 int N, I; 82 bool F = false; // No two identical snowflakes are found, 83 scanf ("% d", & N); 84 while (n --) {85 hashnode * P = new hashnode; 86 p-> next = NULL; 87 for (I = 0; I <6; I ++) // read the length of the 6 petals of this snowflake. 88 scanf ("% d", & P-> Len [I]); 89 If (iscom (p) {// determines whether the snowflake is the same as the previous one. If yes, change the value of F. 90 // The function inserts the information of this petal into the hash table. 91 F = true; 92 break; // if no input is completed, exit midway through, saves 300 ms 93} 94} 95 // determines whether two identical snowflakes have been found 96 If (f) 97 printf ("twin snowflakes found. \ n "); 98 else 99 printf (" no two snowflakes are alike. \ n "); 100 return 0; 101}
Freecode: www.cnblogs.com/yym2013