结构体:HASH表模板

这种 HASHMAP 就是一个链式前向星的表;

其中:

init 函数:hashmap 创建初始化;

check 函数:寻找 hash 表中是否有需要查找的值,若有则返回 1 ,否则返回 0 ;遍历方式与链式前向星一样;

insert 函数:向 hash 表中加入新的 hash 值,若原本就有该值则返回 1 ,否则返回 0 并添加结点;添加方式与链式前向星一样;

 1 #include<stdio.h>
 2 #define ll long long
 3 
 4 const int MAXM=100000007;
 5 
 6 struct{
 7     int next[MAXM],head[MAXM],size;
 8     ll state[MAXM];
 9 
10     void init(){
11         size=0;
12         memset(head,-1,sizeof(head));
13     }
14 
15     bool check(ll val){
16         int h=(val%MAXM+MAXM)%MAXM;
17         for(int i=head[h];~i;i=next[i]){
18             if(state[i]==val)return 1;
19         }
20         return 0;
21     }
22 
23     bool insert(ll val){
24         int h=(val%MAXM+MAXM)%MAXM;
25         for(int i=head[h];~i;i=next[i]){
26             if(state[i]==val)return 1;
27         }
28         state[size]=val;
29         next[size]=head[h];
30         head[h]=size++;
31         return 0;
32     }
33 }H1,H2;
View Code

具体用法见 hdu 5183

原文地址:https://www.cnblogs.com/cenariusxz/p/4331307.html