- const int MAXN=10010;
- const int HASH=10100; //需要hash的数的总个数最大值
- struct HASHMAP
- {
- int head[HASH];
- int next[MAXN];
- int size;
- int state[MAXN];
- void init()
- {
- size=0;
- memset(head,-1,sizeof(head));
- }
- int push(int st)
- {
- int i,h=st%HASH;
- for(i=head[h];i!=-1;i=next[i])
- if(state[i]==st)
- return i;
- state[size]=st;
- next[size]=head[h];
- head[h]=size++;
- return size-1;
- }
- }hm;
以上代码实现功能与下面一样,但速度快许多:
- map<int,int>mp;
- int tol=0;
- int insert(int x)
- {
- if(mp.find(x)==mp.end())mp[x]=tol++;
- return mp[x];
- }