KMP算法

KMP算法


kmp算法是在字符串匹配目前效率最高的算法。傳統的字符串匹配算法時間複雜度爲O(m * n),而kmp算法將時間複雜度簡化到了O(m + n),利用了O(n) 的空間複雜度。

其中,求出next數組是KMP算法的關鍵

// The premier procedure in KPM
    // Set array next
    private static void setNext(String matched, int[] next) {
        next[0] = -1;
        int j = 0;
        int k = -1;
        while (j < matched.length() - 1) {
            // matched.charAt(j) is prefix
            // matched.charAt(k) is postfix
            if (k == -1 || matched.charAt(j) == matched.charAt(k)) {
                ++ j;
                ++ k;
                next[j] = k;
            }
            else {
                k = next[k];
            }
        }

        System.out.println(Arrays.toString(next));

}

例如ABCDABD對應next:




程序實現如下:

import java.util.Arrays;



/**
 * Author     : WindAsMe
 * File       : kpmMatch.java
 * Time       : Create on 18-6-15
 * Location   : ../Home/JavaForLeeCode2/kpmMatch.java
 * Function   : Learning String matched algorithm
 */
public class kpmMatch {

    // KPM matched Algorithm:
    // 1. Calculate the next Array
    // 2. Match and move the text
    // 3. IF match the part of matched and the next is unmatched:
    //    j = next[j] + 1 (j is index the matched)
    // 4. IF j == matched.length() - 1, match ending, success.
    private static int kpmMatchResult(String text, String matched) {
        if (text.length() < matched.length()) {
            return -1;
        }
        int[] next = new int[matched.length()];
        setNext(matched, next);
        int j = 0;
        for (int i = 0 ; i < text.length() ; i ++ ) {
            System.out.println("i:" + i + "  j:" + j);
            if (text.charAt(i) == matched.charAt(j)) {
                j ++;
                if (j == matched.length() - 1) {
                    return i - j + 1;
                }
            } else {
                if (j != 0) {
                    j = next[j] + 1;
                }
            }
        }
        return -1;
    }


    // The premier procedure in KPM
    // Set array next
    private static void setNext(String matched, int[] next) {
        next[0] = -1;
        int j = 0;
        int k = -1;
        while (j < matched.length() - 1) {
            // matched.charAt(j) is prefix
            // matched.charAt(k) is postfix
            if (k == -1 || matched.charAt(j) == matched.charAt(k)) {
                ++ j;
                ++ k;
                next[j] = k;
            }
            else {
                k = next[k];
            }
        }
        System.out.println(Arrays.toString(next));
    }

    public static void main(String[] args) {
        System.out.println(kpmMatchResult("abcweaabcdabcqqababc", "abcabc"));
    }

}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章