Hash learned today. I have not understood the truth yet. I have time to summarize the knowledge points tomorrow. Today I will write a summary report!
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,Join Addition MethodIt is easy to calculate the key value. The link address method can be used in C ++ to solve conflicts, and the vector container can be easily implemented.
Next we will first introduce the vector container: (in fact, today we are also the first to contact the vector container)
1. Basic operations
(1) header file # include <vector>.
(2) create a vector object, vector <int> VEC;
(3) Insert numbers at the end: Vec. push_back ();
(4) use a subscript to access the element. cout <VEC [0] <Endl; remember that the subscript starts from 0.
(5) use an iterator to access elements.
vector<int>::iterator it;for(it=vec.begin();it!=vec.end();it++) cout<<*it<<endl;
(6) Insert element: Vec. insert (VEC. Begin () + I, a); insert a before the I + 1 element;
(7) delete element: Vec. Erase (VEC. Begin () + 2); Delete 3rd Elements
VEC. Erase (VEC. Begin () + I, VEC. End () + J); Delete interval [I, J-1]; interval starts from 0
(8) vector size: Vec. Size ();
(9) clear: Vec. Clear ();
Specific ideas:
Only when the two pieces of snowflake have the same key value can the two pieces of snowflake be the same.
After finding a set of key values, find whether the same snowflake exists in the container of the key value, and then break it out.
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.
Have you heard so much about it? Hey, add the following code:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <vector> 5 #include <algorithm> 6 using namespace std; 7 #define maxn 100010 8 #define pri 100007 9 vector<int> hash[pri];10 int snow[maxn][6];11 int n;12 int cmp(int a,int b)13 {14 for(int i=0; i<6; i++)15 {16 if(snow[a][0]==snow[b][i%6] )17 if(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])18 return 1;19 if(snow[a][5]==snow[b][i%6])20 if(snow[a][4]==snow[b][(i+1)%6] && snow[a][3]==snow[b][(i+2)%6] && snow[a][2]==snow[b][(i+3)%6] && snow[a][1]==snow[b][(i+4)%6] && snow[a][0]==snow[b][(i+5)%6])21 return 1;22 }23 return 0;24 }25 int main()26 {27 scanf("%d",&n);28 int i,j;29 for(i=0; i<n; i++)30 {31 scanf("%d%d%d%d%d%d",&snow[i][0],&snow[i][1],&snow[i][2],&snow[i][3],&snow[i][4],&snow[i][5]);32 }33 int sum,flag=1,key;34 for(i=0; i<n&&flag!=0; i++)35 {36 sum=0;37 for(j=0; j<6; j++)38 sum+=snow[i][j];39 key=sum%pri;40 for(j=0; j
There is a meaning for wings that cannot fly, it's a previous memory of when you once flew through the sky.