Kmp-algorithm-with-rust

最近觉得Rust很好玩, 顺便写了一个Rust版本的Kmp算法。

 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
fn main() {
    let main_str = String::from("ababcaaabaababacbaba");
    let small_str = String::from("ca");
    let pos = find_pos_by_kmp(&main_str, &small_str);
    println!("find pos = {}", pos)
}

fn find_pos_by_kmp(s: &String, t: &String) -> i32 {
    let next = build_next_arr(t);

    return find_pos_with_next_arr(s, t, &next[..]);
}

fn find_pos_with_next_arr(s: &String, t: &String, next: &[usize]) -> i32 {
    let length_t = t.len();
    let length_s = s.len();
    let t = t.as_bytes();
    let s = s.as_bytes();
    let mut x = 0;
    let mut i = 0;

    while i < length_s && x < length_t {
        // println!("i = {}, x = {}", i, x);
        if s[i] == t[x] {
            i += 1;
            x += 1;
        } else if x > 0 {
            x = next[x - 1];
        } else {
            i += 1;
        }
    }
    if x == length_t {
        // find the pos
        return (i - x) as i32;
    }
    return -1;
}

fn build_next_arr(t: &String) -> Vec<usize> {
    let mut v: Vec<usize> = vec![];
    v.push(0);
    let length = t.len();
    let t = t.as_bytes();
    let mut x = 0;
    let mut i = 1;
    while i < length {
        if t[i] == t[x] {
            v.push(x + 1);
            i += 1;
            x += 1;
        } else if x > 0 {
            x = v[x - 1];
        } else {
            v.push(0);
            i += 1;
        }
    }
    return v;
}