【MOBAN】手写HASH表模板
连手写hash表现在都不会,真是太弱了orz (update:为节省空间,添加了count函数)
写法几乎和前向星一模一样(这里用ll是因为hash函数是unsigned long long + 自然溢出)
struct hsh { static const int Ha = 999917,maxe = 10000000; int la[Ha+5],owo,nt[maxe]; ll en[maxe]; int w[maxe],sta[maxe],top; void add(int x,ll y) { if(la[x]==0)sta[++top]=x; en[++owo] = y; nt[owo] = la[x]; la[x] = owo; w[owo]=0; } void clear() { owo = 0; while(top) {la[sta[top--]] = 0;} } bool count(ll x) { int o = x%Ha; for(int it=la[o];it;it=nt[it]) { if(en[it]==x) return true; } return false; } int &operator[] (ll x) { int o = x%Ha; for(int it=la[o];it;it=nt[it]) { if(en[it]==x) return w[it]; } add(o,x); return w[owo]; } }ma;