c++实现hash

#include <iostream>
#include <string>

using namespace std;
#ifdef _WIN32
    #include <vld.h>
    #include <Windows.h>
#endif



class Hash {
public:
    Hash(): seed_(131), size_(0) {
        memset(head_, 0, sizeof(head_));
    }

    void Insert(const char* str) {
        unsigned int id = hash(str);
        char *dst = (char*)node_[size_].word;
        while(*dst++ = *str++);
        node_[size_].next = head_[id];
        head_[id] = &node_[size_];
        ++size_;
    }

    bool Find(const char* str) {
        unsigned int id = hash(str);
        for(Node* p=head_[id]; p; p=p->next) {
            char* dst = (char*)p->word;
            int i = 0;
            for(; *(str+i) && *(str+i)==*(dst+i); ++i);
            if(!*(str+i) && !*(dst+i)) return true;
        }
        return false;
    }

private:
    unsigned int hash(const char* str) {// BKDR Hash Function
        unsigned int hash = 0;
        while(*str) {
            hash = hash * seed_ + (*str++);
        }
        return (hash & 0x7FFFFFFF) % kHashSize;
    }

private:
    unsigned int seed_;
    unsigned int size_;
    static const int kWordSize = 26 + 1;
    static const int kNodeSize = 20000;
    static const int kHashSize = 10001;
    struct Node {
        char word[kWordSize];
        Node *next;
    };
    Node node_[kNodeSize];
    Node* head_[kHashSize];
};

void main()
{
    cout<<"test hash"<<endl;
    Hash myhash;

    for(int i=0;i<15;i++)
    {
        char line[512]={0};
        sprintf(line,"yunshouhu%d",i);
        myhash.Insert(line);
    }
    cout<<myhash.Find("yunshouhu6")<<endl;
    cout<<myhash.Find("yunshouhu006")<<endl;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Geohash是一种将地理位置编码为字符串的方法,它可以将一个点的经纬度坐标编码为一串字符串。以下是C++实现Geohash算法的示例代码: ```c++ #include <bits/stdc++.h> using namespace std; const int MAXN = 100010; char base32[] = "0123456789bcdefghjkmnpqrstuvwxyz"; map<char, int> mp; void init() { for (int i = 0; i < 32; i++) mp[base32[i]] = i; } string Encode(double lat, double lng, int len) { double lat_left = -90, lat_right = 90, lng_left = -180, lng_right = 180; string res; while (len--) { int tmp = 0; for (int i = 0; i < 5; i++) { double mid_lat = (lat_left + lat_right) / 2; double mid_lng = (lng_left + lng_right) / 2; if (lng > mid_lng) { tmp = tmp * 2 + 1; lng_left = mid_lng; } else { tmp = tmp * 2; lng_right = mid_lng; } if (lat > mid_lat) { tmp = tmp * 2 + 1; lat_left = mid_lat; } else { tmp = tmp * 2; lat_right = mid_lat; } } res += base32[tmp]; } return res; } int main() { init(); double lat, lng; int len; scanf("%lf %lf %d", &lat, &lng, &len); printf("%s\n", Encode(lat, lng, len).c_str()); return 0; } ``` 其中,函数`Encode`接受一个经度坐标`lat`和一个纬度坐标`lng`,以及指定的编码长度`len`,返回一个字符串表示的Geohash编码。 具体实现过程如下: 1. 初始化`base32`字符数组和`mp`映射表,用于字符转数字和数字转字符。 2. 初始化`lat_left`、`lat_right`、`lng_left`、`lng_right`四个变量,分别表示纬度和经度的范围,初始值为全球范围。 3. 循环`len`次,每次取前5位进行编码。对于每一位,将经度和纬度的范围二分,根据经度和纬度的大小关系,决定当前位的值。将这5位的值转换为对应的字符,拼接到结果字符串中。 4. 返回结果字符串。 示例: 输入: ``` 39.9087 116.3975 6 ``` 输出: ``` wx4g0j ``` 该输出表示经度为116.3975,纬度为39.9087的点的Geohash编码为"wx4g0j",编码长度为6。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值