五大匹配算法

五大匹配算法

五大匹配算法 BF 匹配、RK 匹配、KMP 匹配、BM 匹配、Sunday 匹配。


五大匹配算法_第1张图片

1、BF 匹配

// BF 匹配(暴力匹配)
public static int bfMatch(String text, String pattern) {
    char[] t = text.toCharArray();
    char[] p = pattern.toCharArray();
    int i = 0, j = 0;
    while (i < t.length && j < p.length) {
        if (t[i] == p[j]) {
            i++;
            j++;
        } else {
            i = i - j + 1;
            j = 0;
        }
    }
    if (j == p.length) {
        return i - j;
    } else {
        return -1;
    }
}

2、RK 匹配

// RK 匹配(哈希匹配)
public static int rkMatch(String text, String pattern) {
    char[] t = text.toCharArray();
    char[] p = pattern.toCharArray();
    int pHash = hash(p, 0, p.length);
    for (int i = 0; i < t.length - p.length + 1; i++) {
        int tHash = hash(t, i, p.length);
        if (tHash == pHash) {
            int j, k;
            for (j = i, k = 0; k < p.length; ) {
                if (t[j] == p[k]) {
                    j++;
                    k++;
                } else {
                    break;
                }
            }
            if (k == p.length) {
                return i;
            }
        }
    }
    return -1;
}

public static int hash(char[] chars, int startIndex, int length) {
    int r = 26;
    int k = 1225;
    int hash = 0;
    for (int i = startIndex; i < startIndex + length; i++) {
        hash = r * hash + chars[i] % k;
    }
    return hash;
}

3、KMP 匹配

// KMP 匹配
public static int kmpMatch(String text, String pattern) {
    char[] t = text.toCharArray();
    char[] p = pattern.toCharArray();
    int[] next = next(p);
    int i = 0, j = 0;
    while (i < t.length && j < p.length) {
        if (j == -1 || t[i] == p[j]) {
            i++;
            j++;
        } else {
            j = next[j];
        }
    }
    if (j == p.length) {
        return i - j;
    } else {
        return -1;
    }
}

public static int[] next(char[] p) {
    int[] next = new int[p.length];
    int k = -1, j = 0;
    next[j] = -1;
    while (j < p.length - 1) {
        if (k == -1 || p[k] == p[j]) {
            k++;
            j++;
            if (p[k] == p[j]) {
                next[j] = next[k];
            } else {
                next[j] = k;
            }
        } else {
            k = next[k];
        }
    }
    return next;
}

4、BM 匹配

// BM 匹配
public static int bmMatch(String text, String pattern) {
    char[] t = text.toCharArray();
    char[] p = pattern.toCharArray();
    int[] ascii = ascii(p);
    for (int i = 0, j; i < t.length - p.length + 1; ) {
        for (j = p.length - 1; t[i + j] == p[j]; j--) {
            if (j == 0) {
                return i;
            }
        }
        i += Math.max(1, j - ascii[t[i + j]]);
    }
    return -1;
}

public static int[] ascii(char[] p) {
    int[] ascii = new int[256];
    for (int i = 0; i < p.length; i++) {
        ascii[p[i]] = i;
    }
    return ascii;
}

5、Sunday 匹配

// Sunday 匹配
404

@XGLLHZ - 陈慧娴 -《跳舞街》.mp3

你可能感兴趣的:(数据结构与算法,算法,哈希算法,散列表,匹配算法,java)