您的位置:首页 > 理论基础 > 数据结构算法

Snowflake Snow Snowflakes(hash)

2015-10-08 23:21 369 查看
F - Snowflake Snow SnowflakesTime Limit:4000MS     Memory Limit:65536KB     64bit IO Format:%I64d& %I64uSubmit Status Practice POJ3349Appoint description: System Crawler  (2015-10-03)DescriptionYou 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 snowflakehas 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.InputThe 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 sixintegers (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.OutputIf 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.
思路:
一开始根本不知道怎样做,之后暴力写了一次结果TLE了。最后看到别人的代码是用hash表做的,最后在终于懂了。这题要找的雪花是否存在6个角都相等,没先后顺序之分。这题就是将所有的六个角的值加起来再取余任意一个大素数即可。将所得的和看做一个大的容器,将所有符合的都放进去,之后就可以只取细分后的容器而不用逐个求了。
AC代码:
#include<iostream>#include<algorithm>#include<cstdlib>#include<cstring>#include<string>#include<cstdio>#include<vector>#include<cmath>#include<map>using namespace std;#define T 15001#define inf 0x3f3f3f3f#define CRL(a) memset(a,0,sizeof(a))typedef long long ll;struct node{int floor[7];}p;node s[T][1000];int t[T];bool sovle(node& a,node& b){sort(a.floor,a.floor+6);sort(b.floor,b.floor+6);for(int i=0;i<6;++i){if(a.floor[i]!=b.floor[i])return false;}return true;}int main(){/*freopen("input.txt","r",stdin);*/int n,i,j,k,ok;while(~scanf("%d",&n)){CRL(t);ok=0;while(n--){for(i=0,k=0;i<6;++i){scanf("%d",&p.floor[i]);k=(k+p.floor[i])%T;}if(!ok){for(i=0;i<t[k];++i){if(sovle(p,s[k][i])){ok=1;break;}}s[k][t[k]]=p;t[k]++;}}if(ok)printf("Twin snowflakes found.\n");elseprintf("No two snowflakes are alike.\n");}return 0;}

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  hash 数据结构