zoukankan      html  css  js  c++  java
  • POJ 3349 Snowflake Snow Snowflakes

    题意:一个长度为6的序列表示一片雪花的六个胳膊(?)的长度,给出n个雪花,问存不存在两片一样的,一样的就是两个雪花旋转或者翻转后胳膊的长度是一样的……

    解法:哈希。以前并没有做过对数字序列哈希……先用map搞了搞,然后T了……百度了一下,只要将序列元素和进行哈希就可以了,对于索引的选取,一开始选了10n+7,然而T了……不是说越大哈希完了越分散么……想了想觉得是因为用vector搞的初始化太耗时……于是把索引减小吧……5003是个不错的选择……嗯?我怎么知道的?哈哈哈哈当然是我过于机智……嗯?你说什么?

    …………才……才不是试出来的!

    好吧……

    如果不用vector的话应该可以更优化……但是用vector实在太方便了嘤嘤嘤_(:з」∠)_

    代码:

    #include<stdio.h>
    #include<iostream>
    #include<algorithm>
    #include<string>
    #include<string.h>
    #include<math.h>
    #include<limits.h>
    #include<time.h>
    #include<stdlib.h>
    #include<map>
    #include<queue>
    #include<set>
    #include<stack>
    #include<vector>
    #define LL long long
    using namespace std;
    const int mod = 5003;
    const int N = 5010;
    struct node
    {
        int a[6];
    };
    vector <node> sf[N];
    bool isequal(node a, node b)
    {
        for(int i = 0; i < 6; i++)
        {
            int flag = 1;
            for(int j = 0; j < 6; j++)
                if(a.a[j] != b.a[(j + i) % 6])
                {
                    flag = 0;
                    break;
                }
            if(flag) return true;
            flag = 1;
            for(int j = 0; j < 6; j++)
                if(a.a[j] != b.a[(11 - i - j) % 6])
                {
                    flag = 0;
                    break;
                }
            if(flag) return true;
        }
        return false;
    }
    int main()
    {
        int n;
        while(~scanf("%d", &n))
        {
            for(int i = 0; i < N; i++)
                sf[i].clear();
            bool ans = 0;
            for(int i = 0; i < n; i++)
            {
                node tmp;
                int sum = 0;
                for(int j = 0; j < 6; j++)
                {
                    scanf("%d", &tmp.a[j]);
                    sum += tmp.a[j];
                }
                sum %= mod;
                if(sf[sum].size())
                {
                    int len = sf[sum].size();
                    for(int j = 0; j < len & !ans; j++)
                        if(isequal(tmp, sf[sum][j])) ans = 1;
                }
                sf[sum].push_back(tmp);
            }
            if(ans) puts("Twin snowflakes found.");
            else puts("No two snowflakes are alike.");
        }
        return 0;
    }
    

      

  • 相关阅读:
    android之StrictMode介绍
    m3u8介绍
    Spring笔记3
    android之常用命令(未完待续)
    JAVA核心技术
    Struts2笔记2
    android之lint警告This Handler class should be static or leaks might occur
    [Algorithm]01分数规划——Update:2012年7月27日
    asp生成html静态
    图片放大缩小
  • 原文地址:https://www.cnblogs.com/Apro/p/4802033.html
Copyright © 2011-2022 走看看