Corresponding POJ topic: Click to open link
Snowflake Snow Snowflakes
Time Limit: 4000MS |
|
Memory Limit: 65536K |
Total Submissions: 33595 |
|
Accepted: 8811 |
Description
You may have heard and snowflakes are alike. Your task is to write a program to determine whether the is really true. Your program would read information about a collection of the snowflakes, and search for a pair, the May is identical. Each snowflake have six arms. For each snowflake, your program is provided with a measurement of the length of each of the six arms. Any pair of snowflakes which has the same lengths of corresponding arms should be flagged by your program as possibly IDE Ntical.
Input
The first line of input would contain a single integer n, 0 < n ≤100000, and the number of snowflakes to Follow. This is being followed by n lines and each describing a snowflake. Each snowflake would be described by a line containing six integers (each integer was at least 0 and less than 10000000), th e lengths of the arms of the snow Ake. The lengths of the arms would be given on order around the snowflake (either clockwise or counterclockwise), but they may b Egin with any of the six arms. For example, the same snowflake could is described as 1 2 3 4 5 6 or 4 3 2 1 6 5.
Output
If all of the snowflakes is distinct, your program should print the message:
No. Snowflakes is alike.
If There is a pair of possibly identical snow akes, your program should print the message:
Twin snowflakes found.
Sample Input
21 2 3 4 5 64 3 2 1 6 5
Sample Output
Twin snowflakes found.
Test instructions
A snowflake has 6 leaves, give the n flake snowflakes, the following 6 numbers per line, respectively, representing the length of each leaf, asked if there are 2 pieces of snowflake shape is the same.
Definition of the same pattern: for example, 2 3 4 5 6 1 and 4 5 6 1 2 3 and 3 2 1 6 5 4 All are the same snowflake, that is the same piece of snowflakes.
That is, a snowflake can start at a certain number of clockwise or counterclockwise numbers.
Ideas:
Add all the lengths of a snowflake mod a large prime number (around 100w) as the key value key, then only the same snowflake can be the same shape. While the same snowflake will be mapped to the same slot in the hash table, we use the linked list to the same key value in the same slot together, only the key is equal to each other when the key value of the chain of snowflakes exist in the same shape, if there is a mark, and then just read the data is not compared If not, add snowflakes to the list. Can also adopt the method of root-based addressing, the idea is the same. Compare snowflake patterns for equality between clockwise and counterclockwise
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <string> #include < algorithm> #include <map> #include <iostream>using namespace std; #define N 100007const int prime = 999983; typedef struct{int Key;int arm[7];} Point; Point *slot[prime+10];bool Check (Point *p, point *q)//p is new, q is the old {int I, j;//clockwise for (j = 0; J < 6; J + +) {for (i = 0; i < 6; i++) if (q->arm[i]! = p->arm[(i+j)%6]) break;if (6 = = i) return true;} Counterclockwise for (j = 0; J < 6; J + +) {for (i = 0; i < 6; i++) {if (Q->arm[i]! = p->arm[(12-i-j)%6]) break; if (6 = = i) return true;} return false;} int Hash (int k) {/*char s[20];sprintf (s, "%d", k); int i, h = 0, a = 232;for (i = 0; I < strlen (s); i++) H = (A * H + s[i] -' 0 ')% Prime;*/return (k<<2);} BOOL Try_to_insert (point *p) {int k = p->key;if (NULL = = Slot[k]) {//empty slot slot[k] = P;return true;} while (Slot[k]) {if (Check (p, slot[k])) return false;k = (k + Hash (k))% Prime;} Slot[k] = P;return true;} int main () {//freopen ("In.txT "," R ", stdin); int I, J, N, flag = 0; Point *p;scanf ("%d", &n); for (i = 0; i < n; i++) {p = (point *) malloc (sizeof);p->key = 0;for (j = 0; J < 6; J + +) {scanf ("%d", &p->arm[j]);p->key = (P->key + p->arm[j])% prime;} if (flag) continue;if (! Try_to_insert (p)) flag = 1;} if (flag) printf ("Twin Snowflakes found.\n"), Else printf ("No. Snowflakes is alike.\n"); return 0;}
Hash--poj 3349 Snowflake Snow Snowflakes