-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkmp.rs
36 lines (32 loc) · 1.07 KB
/
kmp.rs
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
// Knuth–Morris–Pratt algorithm
pub fn substring(needle: &[u8], haystack: &[u8]) -> Vec<usize> {
let mut result: Vec<usize> = Vec::new();
let mut prefix: Vec<usize> = Vec::with_capacity(needle.len());
prefix.push(0);
let mut last_prefix: usize = 0;
// Init vector of prefixes
for &c in &needle[1..] {
while (last_prefix > 0) && (needle[last_prefix] != c) {
last_prefix = prefix[last_prefix - 1];
}
if needle[last_prefix] == c {
last_prefix += 1;
}
prefix.push(last_prefix);
}
last_prefix = 0;
for (i, &d) in haystack.iter().enumerate() {
while (last_prefix > 0) && (needle[last_prefix] != d) {
last_prefix = prefix[last_prefix - 1];
}
if needle[last_prefix] == d {
if last_prefix + 1 == needle.len() {
result.push(i + 1 - needle.len());
last_prefix = prefix[last_prefix];
} else {
last_prefix += 1;
}
}
}
result
}