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 5Sample 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;}
相关文章推荐
- c语言实现hashmap(转载)
- Lua教程(七):数据结构详解
- Ruby中Hash的11个问题解答
- 解析从源码分析常见的基于Array的数据结构动态扩容机制的详解
- C#数据结构揭秘一
- Ruby简明教程之数组和Hash介绍
- 数据结构之Treap详解
- 在C#中生成与PHP一样的MD5 Hash Code的方法
- js中hash和ico的关联分析
- JavaScript数据结构和算法之图和图算法
- Javascript SHA-1:Secure Hash Algorithm
- Java数据结构及算法实例:冒泡排序 Bubble Sort
- 理解php Hash函数,增强密码安全
- Java数据结构及算法实例:插入排序 Insertion Sort
- Java数据结构及算法实例:考拉兹猜想 Collatz Conjecture
- java数据结构之java实现栈
- java数据结构之实现双向链表的示例
- Java数据结构及算法实例:选择排序 Selection Sort
- Java数据结构及算法实例:朴素字符匹配 Brute Force
- Java数据结构及算法实例:汉诺塔问题 Hanoi