Time Limit: 4000MS | Memory Limit: 65536K | |
Total Submissions: 29196 | Accepted: 7674 |
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 should be flagged by your program as possibly identical.
你可能听说过,没有两片雪花是完全相同的。你的任务是写一个程序,以确定这是否是真的。你的程序将读取有关雪花的信息,并搜索一对,可能是相同的。每个雪花有六个花瓣。对于每个雪花,程序将提供与每个六花瓣的长度的测量值。如果花瓣长度相同,则认为是一对相同的花瓣。
Input
The first line of input 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 could be described as 1 2 3 4 5 6 or 4 3 2 1 6 5.
输入的第一行包含一个整数n,0 <n≤100000,雪花的数量。这之后,n行,每一行都描述一个雪花。每个雪花将由含有6个整数(每个整数是至少为0并且小于10000000),代表雪花的六瓣长度。臂的长度可以为顺序围绕雪花(顺时针或逆时针)给出,但它们可开始于任何六个臂。例如,相同的雪花可以被描述为1 2 3 4 5 6或4 3 2 1 6 5。
Output
If all of the snowflakes are distinct, your program should print the message:
No two snowflakes are alike.
If there is a pair of possibly identical snow akes, your program should print the message:
Twin snowflakes found.
Sample Input
2 1 2 3 4 5 6 4 3 2 1 6 5
Sample Output
Twin snowflakes found.
Source
#include <stdio.h>
#include <string.h>
#include <malloc.h>
int n;
const int primer = 999983;
__int64 leaf[100001][6];
struct Hashtable{
__int64 len[6];
struct Hashtable* next;
};
struct Hashtable *hash_t[999984];
__int64 calculate(int i){
__int64 key = 0;
int j;
for (j = 0;j < 6;++ j){
key += leaf[i][j] % primer;
key %= primer;
}
key++;
// printf("key = %I64d\n", key);
return key;
}
int clockwise(struct Hashtable* temp, int i){
int j, k, flag = 0;
for (j = 0; j < 6; ++ j){
flag = 1;
for (k = 0; k < 6; ++ k){
if (leaf[i][k] != temp->len[(j+k) % 6]){
flag = 0;
break;
}
}
if (flag){
return 1;
}
}
//printf("ok2\n");
return 0;
}
int anticlockwise(struct Hashtable* temp, int i){
int j, k, flag = 0;
// for (j = 0;j < 6;++ j){
// printf("temp[%d] = %d\n", j, temp->len[j]);
// }
for (j = 0;j < 6;++ j){
flag = 1;
for (k = 0;k < 6;++ k){
// printf("(5-j-k+6) mod 6 = %d\n", (5-j-k+6) % 6);
// printf("leaf[%d][%d] = %d\n", i, k, leaf[i][k]);
if (leaf[i][k] != temp->len[(j-k+6) % 6]){
// printf("(5-j-k+6) % 6 = %d\n", (5-j-k+6) % 6);
flag = 0;
break;
}
}
if (flag){
return 1;
}
}
return 0;
}
int insert_hash(int i){
int j;
__int64 key = calculate(i);
if (hash_t[key] == NULL){
struct Hashtable* temp=(struct Hashtable*)malloc(sizeof(struct Hashtable));
if (temp == NULL) return -1;//没能分配到空间,出错
for (j = 0;j < 6;j ++){
temp->len[j] = leaf[i][j];
}
hash_t[key] = temp;
temp -> next = NULL;
}else{
struct Hashtable* temp=hash_t[key];
if (clockwise(temp, i) || anticlockwise(temp, i)){
// printf("ok1\n");
return 1;
}
while(temp->next != NULL){
temp = temp->next;
if (clockwise(temp, i) || anticlockwise(temp, i)){
return 1;
}
}
if ((temp->next = (struct Hashtable*)malloc(sizeof(struct Hashtable)))== NULL){
return -1;//没能分配到内存
}
for (j = 0; j < 6; ++ j){
temp->next->len[j] = leaf[i][j];
}
temp = temp->next;temp->next = NULL;
}
return 0;
}
int main(void){
int i, j;
int flag = 0;
freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
memset(hash_t, 0, sizeof(hash_t));
scanf("%d", &n);
for (i = 0;i < n;++ i){
for (j = 0;j < 6;j ++){
scanf("%I64d", &leaf[i][j]);
// printf("%I64d ", leaf[i][j]);
}
// printf("\n");
if (!flag)
flag = insert_hash(i);
}
if (flag){
printf("Twin snowflakes found.\n");
}else{
printf("No two snowflakes are alike.\n");
}
return 0;
}