1. 程式人生 > >簡單好用的雜湊表

簡單好用的雜湊表

雜湊錶板子

以後考試不要總自己發揮妄圖創造模板。。

 1 struct Hsh{
 2 int head[N1],to[M1],nxt[M1],val[M1],cte;
 3 void ins(int x,int w)
 4 {
 5     int u=x%maxn,j,v;
 6     for(j=head[u];j;j=nxt[j])
 7     {
 8         v=to[j];
 9         if(v==x) return;
10     }
11     cte++; to[cte]=x; nxt[cte]=head[u];
12     head[u]=cte; val[cte]=w;
13 } 14 int find(int x) 15 { 16 int u=x%maxn,j,v; 17 for(j=head[u];j;j=nxt[j]) 18 { 19 v=to[j]; 20 if(v==x) return val[j]; 21 } 22 return -1; 23 } 24 };