-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash.cpp
More file actions
55 lines (50 loc) · 1.01 KB
/
Copy pathhash.cpp
File metadata and controls
55 lines (50 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
using namespace std;
const int M = 1000003;
const int a = 1000;
const int MX = 500005;
int head[M];
int pre[MX];
int nxt[MX];
string key[MX];
int val[MX];
int unused = 0;
// rolling hash
int hash_function(string& s){
int h = 0;
for(auto x : s) h = (h*a+x)%M;
return h;
}
int find(string k){
int h = hash_function(k);
int idx = head[h];
while(idx!=-1){
if(key[idx]==k) return idx;
idx = nxt[idx];
}
return -1;
}
void insert(string k, int v){
int idx = find(k);
if(idx!=-1){
val[idx] = v;
return;
}
int h = hash_function(k);
key[unused] = k;
val[unused] = v;
if(head[h]!=-1){
nxt[unused] = head[h];
pre[head[h]] = unused;
}
head[h] = unused;
unused++;
}
void erase(string k){
int idx = find(k);
if(idx==-1) return;
if(pre[idx]!=-1) nxt[pre[idx]] = nxt[idx];
if(nxt[idx]!=-1) pre[nxt[idx]] = pre[idx];
int h = hash_function(k);
if(head[h]==idx) head[h] = nxt[idx];
}