Snowflake Snow Snowflakes

来源:互联网 发布:java程序开发培训价格 编辑:程序博客网 时间:2024/06/08 00:50

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 byn 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

21 2 3 4 5 64 3 2 1 6 5

Sample Output

Twin snowflakes found.


题解:虽然题目不难,但是还是很难。可以用map将每一个雪花的顺时针和逆时针的12中情况映射到同一个与其他雪花不同的key上面,这样的话,只要查找该雪花的健值是否存在就知道了,但是map效率太低了。超时。所以只能用hash了,将所有6个角的和除以一个大数(一般找尽量大的素数)。但是如果数据坑爹任然过不了。。。假如所有的和都一样,那不就是n2了,萎了。幸好数据萎。。。。


#include <iostream>#include <cstdio>#include <cstring>#include <map>using namespace std;struct Hash{int index;Hash* next;Hash(){index = 0;next = NULL;}};map<int,int> mp;bool flag[11000005];int s[100005][10];Hash hash[1100005];bool ok1(int x,int key)  {for(int i = 0;i < 6;i++){int j;int y;for(j = 0,y = i;j < 6;j++){if(s[x][j] != s[key][y]){break;}y = (++y) % 6;}if(j == 6){return true;}for(j = 0,y = i;j < 6;j++){if(s[x][j] != s[key][y]){break;}y = (--y + 6) % 6;}if(j == 6){return true;}}return false;}bool ok(int x,int key){Hash* p = hash[key].next;while(p != NULL){if(ok1(x,p->index)){return true;}p = p->next;}return false;}int main(){int n;while(scanf("%d",&n) != EOF){//mp.clear();memset(flag,false,sizeof(flag));bool f = false;int k = 1; for(int i = 0;i < n;i++){int t = 0;for(int j = 0;j < 6;j++){scanf("%d",&s[i][j]);t += s[i][j];}int x = t % 958426;if(flag[x]){if(ok(i,x)){printf("Twin snowflakes found.\n");f = true;    continue;}Hash* p = new Hash();p->index = i;p->next = hash[x].next;hash[x].next = p;}else{//mp[t] = k++;Hash* p = new Hash();p->index = i;p->next = hash[x].next;hash[x].next = p;flag[x] = true;}}if(!f){printf("No two snowflakes are alike.\n");}}return 0;}


0 0
原创粉丝点击