Determine whether there are two identical snowflakes. Each snowflake has six petals. Compare the length of the petals to see if they are the same.ArmsThe same length is the same. GivenNAs long as there are two identical onesTwin snowflakes found.If either of the two outputs is different, no two snowflakes are alike. N = 100,000.
Thought: the simplest thing is to enumerate each two pieces of Snowflake and determine whether they are the same. The time complexity is O (n * n), which is obviously not ideal. Is there any better algorithm? Hash: each time a snowflake is read, the snowflake hash is used to determine whether the hash table has the same hash value and the same hash value. The hash value is extracted from the linked list one by one and determined whether it is homogeneous, is homogeneous to obtain the result. Then add the snowflake to the table. If all the snowflakes have not read the same one, the result is obtained.
The Code is as follows:
# Include <stdio. h> # include <stdlib. h ># include <vector >#include <iostream> using namespace STD; const int max_size = 100005; // The maximum number of snowflakes const int mod_val = 90001; // hash function, number of remainder int snow [max_size] [6]; // stores snowflake information vector <int> hash [mod_val]; // hash table, the table stores the subscript of the snow array/* determines whether snowflake A and snowflake B are the same * input: The subscript of two snowflakes in the snow array * output: true or false */bool issame (int A, int B) {for (INT I = 0; I <6; I ++) {If (/* clockwise */(snow [a] [0] = snow [B] [I] & Snow [a] [1] = snow [B] [(I + 1) % 6] & Snow [a] [2] = snow [B] [(I + 2) % 6] & Snow [a] [3] = snow [B] [(I + 3) % 6] & Snow [a] [4] = snow [B] [(I + 4) % 6] & Snow [a] [5] = snow [B] [(I + 5) % 6]) |/* */(snow [a] [0] = snow [B] [I] & Snow [a] [1] = snow [B] [(I + 5) % 6] & Snow [a] [2] = snow [B] [(I + 4) % 6] & Snow [a] [3] = snow [B] [(I + 3) % 6] & Snow [a] [4] = snow [B] [(I + 2) % 6] & Snow [a] [5] = snow [B] [(I + 1) % 6]) return true;} return false ;} int main () {/* processing Input */int n; int I, j; scanf ("% d", & N); for (I = 0; I <N; I ++) {for (j = 0; j <6; j ++) {scanf ("% d", & Snow [I] [J]);} /* process the N snowflakes separately and determine whether the two snowflakes are the same */INT sum, key; for (I = 0; I <n; I ++) {/* obtain the sum of the six petals of snowflake */SUM = 0; For (j = 0; j <6; j ++) {sum + = snow [I] [J];} key = sum % mod_val; // obtain the key/* to determine whether the snowflake stored in hash [Key] in the hash table is the same as that in Snowflake I */For (vector <int>: size_type J = 0; j