哈希问题:

哈希问题

哈希问题不是很了解:目前就是用一些题目来解释吧:
1.雪花问题:poj3349
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 by n 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.

中文意思:输入n片雪花,判断是否有同分异构体:
代码参考:博客http://blog.csdn.net/y990041769/article/details/20560433

这里的代码很巧妙,用一个二维数组来替代哈希表:

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>

int m[100001];

using namespace std;

const int mod = 99991;

struct Node
{
    int floor[6];
    //Node *next;
};

Node hash1[1000001];

bool cmp(Node x,Node y)
{
    sort(x.floor,x.floor+6);
    sort(y.floor,y.floor+6);
    for(int i=0;i<6;i++)
    {
        if(x.floor[i] != y.floor[i])
            return false;
    }
    return true;
}

Node snow[100001][100];
/*void Hash(int key)
{
    if(!m[key])
    {
        m[key] = 1;
        Node *temp = new Node;
        temp->floor =
    }
}*/


int main()
{
    int n;
    while(cin >> n)
    {
        int ok=0;
       memset(m,0,sizeof(m));
       for(int i=0;i<n;i++)
       {

           Node pp;
           int sum=0;

           for(int j=0;j<6;j++)
           {
               cin >> pp.floor[j];
               sum = (sum + pp.floor[j]) % mod;
           }
           for(int k=0;k<m[sum];k++)//这里将m[sum]=0,就只接进行创建,不需要用新的哈希函数来解决。
           {
               if(cmp(snow[sum][k],pp))
               {
                   ok = 1;
                   break;
               }
           }
           snow[sum][m[sum]] = pp;
           m[sum]++;
       }

       if(ok)
        printf("Twin snowflakes found.\n");
       else
        printf("No two snowflakes are alike.\n");

    }
    return 0;
}

这里就直接给出代码了

2.给出多项式的问题:poj1840
参考:http://user.qzone.qq.com/289065406/blog/1304402322
题目:就是给出5元3次方程:给出一个5元3次方程,输入其5个系数,求它的解的个数
其中系数 ai∈[-50,50] 自变量xi∈[-50,0)∪(0,50]

解题思路
直观的思路:暴力枚举,O(n^5)
题目Time Limit=5000ms,1ms大约可以执行1000条语句,那么5000ms最多执行500W次
每个变量都有100种可能值,那么暴力枚举,5层循环,就是要执行100^5=100E次,等着TLE吧。。。。

要AC这题,就要对方程做一个变形

即先枚举x1和x2的组合,把所有出现过的 左值 记录打表,然后再枚举x3 x4 x5的组合得到的 右值,如果某个右值等于已经出现的左值,那么我们就得到了一个解
时间复杂度从 O(n^5)降低到 O(n^2+n^3),大约执行100W次

我们先定义一个映射数组hash[],初始化为0
对于方程左边,当x1=m , x2= n时得到sum,则把用hash[]记录sum : hash[sum]++,表示sum这个值出现了1次
之所以是记录“次数”,而不是记录“是否已出现”,
是因为我们不能保证函数的映射为 1对1 映射,更多的是存在 多对1映射。
例如当 a1=a2时,x1=m , x2= n我们得到了sum,但x1=n , x2= m时我们也会得到sum,但是我们说这两个是不同的解,这就是 多对1 的情况了,如果单纯记录sum是否出现过,则会使得 解的个数 减少。

其次,为了使得 搜索sum是否出现 的操作为o(1),我们把sum作为下标,那么hash数组的上界就取决于a1 a2 x1 x2的组合,四个量的极端值均为50
因此上界为 50*50^3+50*50^3=12500000,由于sum也可能为负数,因此我们对hash[]的上界进行扩展,扩展到25000000,当sum<0时,我们令sum+=25000000存储到hash[]
由于数组很大,必须使用全局定义

同时由于数组很大,用int定义必然会MLE,因此要用char或者short定义数组,推荐short。
下面给出代码:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>

using namespace std;

const int MAX = 99991;

struct Node
{
    int value;
    Node *next;
};

Node node[MAX];
bool trag[MAX];
int a,b,c,d,e;

int count =0;

void Hash(bool flag,int key)
{
    int temp = abs(key) % MAX;
    if(!flag)
    {
        if(!trag[temp])
           {
              trag[temp] = 1;
                node[temp].value = key;
              node[temp].next = NULL;
           }
        else
        {
            Node *pivot = (Node *)malloc(sizeof(Node));
            pivot->value = key;
            pivot->next = node[temp].next;
            node[temp].next = pivot;
        }
    }
    else
    {
        if(trag[temp])
        {
            Node *pivot = &node[temp];
            while(pivot != NULL)
            {
                if(pivot->value  == key)
                {
                    count++;
                }
                    pivot = pivot->next;

            }
        }
    }
}

int main()
{
    while(~(scanf("%d%d%d%d%d",&a,&b,&c,&d,&e)))
    {
        int x1,x2,x3,x4,x5;
        for(x1=-50;x1<=50;x1++)
        {
            if(!x1)
                continue;
            for(x2=-50;x2<=50;x2++)
            {
                  if(!x2)
                continue;
            int sum = -(a*x1*x1*x1+b*x2*x2*x2);
            Hash(0,sum);
            }

        }
        for(x3=-50;x3<=50;x3++)
        {
            if(!x3)
                continue;
            for(x4=-50;x4<=50;x4++)
            {
                if(!x4)
                    continue;
                for(x5=-50;x5<=50;x5++)
                {
                   if(!x5)
                    continue;
                Hash(1,c*x3*x3*x3+d*x4*x4*x4+e*x5*x5*x5);
                }

            }
        }
        printf("%d\n",count);
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值