Snowflake Snow Snowflakes

tech2024-10-15  1

Snowflake Snow Snowflakes ⁡ \operatorname{Snowflake\ Snow\ Snowflakes} Snowflake Snow Snowflakes

题目链接: POJ 3349 ⁡ \operatorname{POJ\ 3349} POJ 3349

题目大意

就是有很多的雪花片,每一个雪花片有六个瓣(众所周知)。我们告诉你每个雪花片的每个瓣的长度,问你有没有任意两个雪花片是相同的。

我们有没有雪花片相同,就看从任意一个雪花片的瓣开始数,按顺时针或者逆时针排出的一个序列。 然后如果两个雪花片组出来的序列有相同的,那这两个雪花片就是相同的。

题目

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.

输入

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.

输出

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.

样例输入

2 1 2 3 4 5 6 4 3 2 1 6 5

样例输出

Twin snowflakes found.

数据范围

0 < n ≤ 100000 0 < n ≤ 100000 0<n100000 每一个雪花片的瓣的长度都是大于等于 0 0 0 但小于 10000000 10000000 10000000 的整数。

思路

这道题是一道 hash 。

我们就可以累加雪花瓣的长度得出这个雪花的 hash 值(我这里取模用了 14997 14997 14997 这个素数)。 那如果 hash 值一样的,就直接暴力模拟顺时针或者逆时针的每一种摆法,看是否相同。

有一个要注意的就是 hash 值一样的不同雪花可能会有很多个,我们就要用数组来存下来。 (我是这样的, a [ i ] [ j ] [ k ] a[i][j][k] a[i][j][k] 表示 hash 值是 i i i ,是第 j j j 个 hash 值是这个的不同雪花,它的第 k k k 个瓣的长度)

代码

#include<cstdio> #define mo 14997 using namespace std; int n, a[14998][50][7], sum, num, hashnum[14998], tmp[7]; bool yes; int main() { scanf("%d", &n);//读入 for (int i = 1; i <= n; i++) { sum = 0;//初始化 for (int j = 1; j <= 6; j++) { scanf("%d", &tmp[j]);//读入 sum = (sum + tmp[j]) % mo;//hash } if (hashnum[sum]) { for (int tim = 1; tim <= hashnum[sum]; tim++) {//枚举每一个前面hash值与它相同的雪花 for (int j = 1; j <= 6; j++) {//顺时针 yes = 1; num = 1; for (int k = j; num <= 6; k = ((k == 6) ? 1 : (k + 1))) { if (a[sum][tim][num] != tmp[k]) { yes = 0; break; } num++; } if (yes) {//相同 printf("Twin snowflakes found.");//输出 return 0; } } for (int j = 1; j <= 6; j++) {//逆时针 yes = 1; num = 1; for (int k = j; num <= 6; k = ((k == 1) ? 6 : (k - 1))) { if (a[sum][tim][num] != tmp[k]) { yes = 0; break; } num++; } if (yes) {//相同 printf("Twin snowflakes found.");//输出 return 0; } } } } hashnum[sum]++;//记录 for (int j = 1; j <= 6; j++) a[sum][hashnum[sum]][j] = tmp[j]; } printf("No two snowflakes are alike.");//输出 return 0; }
最新回复(0)