zoukankan      html  css  js  c++  java
  • 手写HASHMAP

    手写HASHMAP

    [cpp] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. const int MAXN=10010;  
    2. const int HASH=10100;            //需要hash的数的总个数最大值   
    3. struct HASHMAP  
    4. {  
    5.     int head[HASH];  
    6.     int next[MAXN];  
    7.     int size;  
    8.     int state[MAXN];  
    9.     void init()  
    10.     {  
    11.         size=0;  
    12.         memset(head,-1,sizeof(head));  
    13.     }  
    14.     int push(int st)  
    15.     {  
    16.         int i,h=st%HASH;  
    17.         for(i=head[h];i!=-1;i=next[i])  
    18.            if(state[i]==st)  
    19.              return i;  
    20.         state[size]=st;  
    21.         next[size]=head[h];  
    22.         head[h]=size++;  
    23.         return size-1;  
    24.     }  
    25. }hm;  

    以上代码实现功能与下面一样,但速度快许多:
    [cpp] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. map<int,int>mp;  
    2. int tol=0;  
    3. int insert(int x)  
    4. {  
    5.     if(mp.find(x)==mp.end())mp[x]=tol++;  
    6.     return mp[x];  
    7. }  
  • 相关阅读:
    模拟测试69
    模拟测试68
    模拟测试66
    NOIP模拟测试15
    NOIP模拟测试14
    Gekoo's checker
    NOIP模拟测试13
    替罪羊树模板
    KDTree笔记
    NOIP模拟测试12
  • 原文地址:https://www.cnblogs.com/lupeng2010/p/6489385.html
Copyright © 2011-2022 走看看