Time Limit: 4000MS | Memory Limit: 65536K | |
Total Submissions: 27277 | Accepted: 7197 |
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.
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.
题意:给n个序列,每个序列有六个数字,每个数字的取值范围是[0,10000000],如果n个序列中有任意两个顺时针或逆时针读是一样的就输出Twin snowflakes found.如果任意两个都不同就输出No two snowflakes are alike. 解题思路:将每个序列的六个数的和对大素数取模后存在邻接表中,这样就缩小了可比较序列的范围,只有存在同一个邻接表的两个序列才有可能匹配;
#include<stdio.h>
#include<string.h>
#include<vector>
using namespace std; struct node
{
int sum,id;
};
vector<struct node>head[];
const int p = ;
int a[][]; bool clockwise(int x, int y)//顺时针判断
{
int i,j;
for(i = ; i < ; i++)
{
if(a[x][i] == a[y][])
{
for(j = ; j < ; j++)
if(a[x][(i+j)%] != a[y][j])
break;
if(j >= ) return true;
}
}
return false;
}
bool counterclockwise(int x, int y)//逆时针判断
{
int i,j;
for(i = ; i < ; i++)
{
if(a[x][i] == a[y][])
{
for(j = ; j < ; j++)
if(a[x][(i+j)%] != a[y][-j])
break;
if(j >= ) return true;
}
}
return false;
}
int main()
{
int i,j,n,tmp;
for(i = ; i < ; i++)
head[i].clear();
scanf("%d",&n);
int ok = ;
for(i = ; i < n; i++)
{
int sum = ;
for(j = ; j < ; j++)
{
scanf("%d",&a[i][j]);
sum += a[i][j];
}
tmp = sum%p;//对大素数取模
if(!ok)
{
for(j = ; j < head[tmp].size(); j++)
{
if(head[tmp][j].sum == sum &&(clockwise(head[tmp][j].id,i)||counterclockwise(head[tmp][j].id,i)))
{
ok = ;
break;
}
}
if(ok == ) head[tmp].push_back((struct node){sum,i});
}
}
if(ok == ) printf("Twin snowflakes found.\n");
else printf("No two snowflakes are alike.\n");
return ;
}