哈希算法简介

introduction to HashTable

1.What is hash?

HashTable is a kind of map. The search process in hashtable holds o(1) complexity. You may think o(1), that’s impossible, since you need to traversal all the container to find the object that you want. However, in hashtable, it’s unnecessary to do so, because you can find the object just using a simple map. Concretely, hash table is an array, you can use index to get the object you want. And index in array holds a complexity of o(1).

2.How to build a hashtable

If we have an enough big array, to load all the statistic you store, then you can easily get the thing you want with an index. Actually, it’s unreal to achieve this kind of hash for when you have a large large number of objects, which are not consistent, then the space cost is quite high. Thus, we need to find a new way to build a hashtable in limited space. But, how can we do that? There are several methods:
- %m with an array(size m)
- fibonacci method

 index = (value * temp) >> 28
 1for short,temp equals to 40503 
 2for int ,temp equals to 2654435769 
 3for long long,temp equals to 11400714819323198485  

As we know that, this kind of map will probably cause crash between or among some maps(say, is not a onto map). Hence, we need to find a way to handle the problems when different objects map to the same index. And the most common way to solve this problem is using linklist. Every hashtable’s entry points to a linklist. Then, if an object get an index same as the other, just push this object to the end of the linklist. When we need to find, just to traverse this linklist to determine whether this object exists.

3.This is a simple hashtable

to find if a string has appeared in the hash table before
code:

#include <iostream>
#include <vector>
//const unsigned long HashValue=11400714819323198485;
using namespace std;
typedef struct Node{
    string name;
    struct Node* next;
}node;
vector<string>input;
node* hashTable[1000];
unsigned long HashString(const char *lpszString)
{
    unsigned long ulHash = 0xf1e2d3c4;
    while (*lpszString != 0)
    {
        ulHash <<= 1;
        ulHash += *lpszString++;
    }
    return ulHash;
}
void IntoHash(const string& string1, unsigned long pos){
    if(!hashTable[pos]){
        auto ptr=new node;
        ptr->name=string1;
        ptr->next=NULL;
        hashTable[pos]=ptr;
    }
    else{
        auto temp=hashTable[pos];
        while(temp->next!=NULL){
            temp=temp->next;
        }
        auto ptr=new node;
        ptr->name=string1;
        ptr->next=NULL;
        temp->next=ptr;
    }
    return;
}
bool Search(const string& string1){
    auto index=HashString(string1.c_str());
    if(hashTable[index%1000]==NULL){
        return 0;
    }
    else{
        auto ptr=hashTable[index%1000];
        do{
            if(ptr->name==string1){
                return 1;
            }
            else{
                ptr=ptr->next;
            }
        }while(ptr!=NULL);
    }
    return 0;
}
int main(void){
    memset(hashTable,NULL,1000);
    int n;
    cin>>n;
    string temp;
    for(int i=0;i<n;i++){
        cin>>temp;
        input.push_back(temp);
    }
    for(const auto i:input){
        unsigned long pos=HashString(i.c_str());
        auto index=pos%1000;
        //unsigned long index = (pos * HashValue) >> 28;
        IntoHash(i,index);
    }
    int m;
    cin>>m;
    for(int i=0;i<m;i++){
        cin>>temp;
        if(Search(temp)){
            cout<<"find"<<endl;
        }
        else{
            cout<<"opps,not exists"<<endl;
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值