-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHash.java
More file actions
100 lines (87 loc) · 2.05 KB
/
Hash.java
File metadata and controls
100 lines (87 loc) · 2.05 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//From HarmeyerHash
static class Hash implements Comparable<Hash> {
static final long m1=8675309, m2=1_000_000_007;
long v1=0, v2=0; int l=0;
static final long s1=257, s2=619;
static long[] s1Pow, s2Pow;
static boolean precomped=false;
void add(char o) {
v1=(v1*s1+o)%m1;
v2=(v2*s2+o)%m2;
l++;
}
void add(int o) {
v1=(v1*s1+o)%m1;
v2=(v2*s2+o)%m2;
l++;
}
public int compareTo(Hash o) {
if (v1!=o.v1)
return Long.compare(v1, o.v1);
return Long.compare(v2, o.v2);
}
public boolean equals(Object o) {
return compareTo((Hash)o)==0;
}
public int hashCode() {
return (int)v1;
}
public Hash clone() {
Hash toReturn=new Hash();
toReturn.v1=v1;
toReturn.v2=v2;
toReturn.l=l;
return toReturn;
}
static void precomp() {
if (precomped) return;
precomped=true;
s1Pow=new long[1000_000];
s2Pow=new long[1000_000];
s1Pow[0]=s2Pow[0]=1;
for (int i=1; i<s1Pow.length; i++)
s1Pow[i]=(s1Pow[i-1]*s1)%m1;
for (int i=1; i<s2Pow.length; i++)
s2Pow[i]=(s2Pow[i-1]*s2)%m2;
}
//need fastPow if o can be longer than 10^6
void append(Hash o) {
precomp();
v1=(v1*s1Pow[o.l]+o.v1)%m1;
v2=(v2*s2Pow[o.l]+o.v2)%m2;
l+=o.l;
}
public static Hash[] getPrefixHashes(char[] word) {
precomp();
int n=word.length;
Hash[] toReturn=new Hash[n+1];
toReturn[0]=new Hash();
for (int i=1; i<=n; i++) {
toReturn[i]=toReturn[i-1].clone();
toReturn[i].add(word[i-1]);
}
return toReturn;
}
public static Hash[] getPrefixHashes(String word) {
precomp();
int n=word.length();
Hash[] toReturn=new Hash[n+1];
toReturn[0]=new Hash();
for (int i=1; i<=n; i++) {
toReturn[i]=toReturn[i-1].clone();
toReturn[i].add(word.charAt(i - 1));
}
return toReturn;
}
//inclusive, exclusive
public static Hash substringHash(Hash[] prefixHashes, int from, int to) {
if (from==to)
return new Hash();
Hash old=prefixHashes[to].clone(), toSub=prefixHashes[from];
int diff=to-from;
old.v1=(old.v1-(toSub.v1*s1Pow[diff])%m1+m1)%m1;
old.v2=(old.v2-(toSub.v2*s2Pow[diff])%m2+m2)%m2;
old.l=to-from;
return old;
}
}