The same snowflake Time limit: 1000 MS | memory limit: 65535 KB difficulty: 4
-
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 the input will contain in a single interger T (0 <T <10), the number of the test cases.
The first line of every test case will contain a single integer n, 0 <n ≤ 100000, the number of snowflakes to follow. this will be followed by n lines, 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
-
For each test case, 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.
-
Example input
-
121 2 3 4 5 64 3 2 1 6 5
-
Example output
-
Twin snowflakes found.
The snowflake has six horns assigned to them respectively. Enter them clockwise and ask if the snowflake you entered is completely the same.
Analysis: according to the traditional practice, the time is O (n ^ 2). Because the data is very large, it will time out. Another method is required, you need to use the hash (which is very specific, and I am ugly ).
This question is also quite strange.
Code 1 (linked list ):
# Include <cstdio> # include <cstring> # define M 20005 using namespace std; struct node {int a [6]; struct node * next;/* data */}; node * s [M]; int match (int * temp, int sum) {int I, j; node * p; p = s [sum]-> next; while (p) {for (I = 0; I <6; ++ I) {for (j = 0; j <6; ++ j) {if (temp [j]! = P-> a [(I + j) % 6]) break;} if (j = 6) return true; for (j = 0; j <6; ++ j) {if (temp [j]! = P-> a [(I + 6-j) % 6]) break;} if (j = 6) return true;} p = p-> next ;} p = new node; for (I = 0; I <6; ++ I) p-> a [I] = temp [I]; p-> next = s [sum]-> next; s [sum]-> next = p; return false;} int main () {int t, n, I, j, temp [6]; scanf ("% d", & t); while (t --) {int sum, flag = 0; scanf ("% d ", & n); for (I = 0; I <M; ++ I) {s [I] = new node; s [I]-> next = NULL ;} while (n --) {sum = 0; for (I = 0; I <6; ++ I) {scanf ("% d", & temp [I]); sum + = t Emp [I];} sum % = M; if (! Flag) {if (match (temp, sum) flag = 1 ;}} if (flag) puts ("Twin snowflakes found. "); else puts (" No two snowflakes are alike. ");} return 0 ;}
Code 2 (3D array ):
# Include <cstdio> # include <cstring> # include <algorithm> using namespace std; # define M 20000int s [M] [100] [6]; int len [M]; int match (int * a, int * B) {int I, j, k; for (I = 0; I <6; I ++) {for (j = 0; j <6; j ++) {if (a [j]! = B [(I + j) % 6]) break;} if (j = 6) return true; for (j = 0; j <6; j ++) {if (a [j]! = B [(I + 6-j) % 6]) break;} if (j = 6) return true;} return false;} int main () {int t, n, sum, temp [6]; scanf ("% d", & t); while (t --) {int I, j, k, flag = 0; scanf ("% d", & n); memset (len, 0, sizeof (int) * (M + 1); while (n --) {sum = 0; for (I = 0; I <6; I ++) {scanf ("% d", & temp [I]); sum + = temp [I];} sum % = M; for (I = 0; I <6; ++ I) {s [sum] [len [sum] [I] = temp [I];} ++ len [sum] ;}for (I = 0; I <M; I ++) {if (len [I]> 1) for (j = 0; j <len [I]-1; j ++) {for (k = j + 1; k <len [I]; k ++) {if (match (s [I] [j], s [I] [k]) {flag = 1; break ;}} if (flag) break ;} if (flag) break;} if (flag) puts ("Twin snowflakes found. "); else puts (" No two snowflakes are alike. ");} return 0 ;}
Nyoj 130 the same snowflake [hash]