poj3349 Snowflake Snow Snowflakes之hash入门

poj3349
hash早有耳闻,然而很少做到相关的题学习之事就一拖再拖,今天遇到了个简单的hash。写了之后对hash有了一定的了解,之后学习就快多了。
题目大意就是让你判断一些6个数字的数组通过循环或者倒置产生相同的结果,直接一个一个判断肯定超时,简单做下hash处理后hash值相同的再进行比较就能过了。
我的处理是求下和,然后取下余。

#include <cstdio>
#include <cstring>
#include <cctype>
#include <cmath>
#include <set>
#include <map>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <string>
#include <bitset>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
const int INF=0x3f3f3f3f;
inline int  ReadIn()
{
    char ch = getchar();
    int data = 0;
    while (ch < '0' || ch > '9') ch = getchar();
    do
    {
        data = data*10 + ch-'0';
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9');
    return data;
}

int n,m;
vector<int>v[100009];
struct node
{
    int egde[7];
};
node nod[100005];
int get_hash(node  a)
{
    int num=0;
    for(int i=0;i<6;i++)
        num+=a.egde[i];
    return num%100007;
}
bool take(int a,int b)
{
    for(int i=0;i<6;i++)
    {
        bool fla=true;
        for(int j=0;j<6;j++)
            if(nod[a].egde[j]!=nod[b].egde[(j+i)%6])
            {
                fla=false;
                break;
            }
            if(fla)
                return true;
    }
    for(int i=0;i<6;i++)
    {
        bool fla=true;
        for(int j=0;j<6;j++)
            if(nod[a].egde[j]!=nod[b].egde[(12-j-i)%6])
            {
                fla=false;
                break;
            }
            if(fla)
                return true;
    }
return false;
}
bool slove(int hash,int pla)
{
    bool foo=false;
    for(int i=0;i<v[hash].size();i++)
    {
        if(take(v[hash][i],pla))
            return true;
    }
v[hash].push_back(pla);
return false;
}
int main()
{
#ifdef LOCAL
    freopen("date.in","r",stdin);
    freopen("date.out","w",stdout);
#endif
        scanf("%d",&n);
    bool fla=false;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<6;j++)
            nod[i].egde[j]=ReadIn();
        if(!fla)
        {
        int pla=get_hash(nod[i]);
        //printf("%d\n", pla);
        fla=slove(pla,i);
        }
    }
    if(fla)
        puts("Twin snowflakes found.");
    else
        puts("No two snowflakes are alike.");

    return 0;

}

你可能感兴趣的:(hash,poj)