java實現sunday演算法樣本分享_java

來源:互聯網
上載者:User

字串匹配尋找演算法中,最著名的兩個是KMP演算法(Knuth-Morris-Pratt)和BM演算法(Boyer-Moore)。兩個演算法在最壞情況下均具有線性尋找時間。但是在實用上,KMP演算法並不比最簡單的C庫函數strstr()快多少,而BM演算法則往往比KMP演算法快上3-5倍(未親身實踐)。但是BM演算法還不是最快的演算法,這裡介紹一種比BM演算法更快一些的尋找演算法Sunday演算法。

Sunday演算法的思想和BM演算法中的壞字元思想非常類似。差別只是在於Sunday演算法在匹配失敗之後,是取目標串中當前和Pattern字串對應的部分後面一個位置的字元來做壞字元匹配。當發現匹配失敗的時候就判斷母串中當前位移量+Pattern字串長度+1處(假設為K位置)的字元在Pattern字串中是否存在。如果存在,則將該位置和Pattern字串中的該字元對齊,再從頭開始匹配;如果不存在,就將Pattern字串向後移動,和母串k+1處的字元對齊,再進行匹配。重複上面的操作直到找到,或母串被找完結束。動手寫了個小例子來實現以下這個演算法。

在代碼中,實現了兩種字串匹配演算法,一種是Sunday方式,一種是普通的每次移動一位的方式,二者的效率對比在main函數中有,都是納秒層級。演算法的詳細步驟,在代碼中已經添加了相應的注釋。關於BM演算法,下次空了再一起對照著分析。

複製代碼 代碼如下:

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @author Scott
 * @date 2013年12月28日
 * @description
 */
public class SundySearch {
    String text = null;
    String pattern = null;
    int currentPos = 0;

    /**
     * 匹配後的子串第一個字元位置列表
     */
    List<Integer> matchedPosList = new LinkedList<Integer>();

    /**
     * 匹配字元的Map,記錄改匹配字串有哪些char並且每個char最後出現的位移
     */
    Map<Character, Integer> map = new HashMap<Character, Integer>();

    public SundySearch(String text, String pattern) {
        this.text = text;
        this.pattern = pattern;
        this.initMap();
    };

    /**
     * Sunday匹配時,用來儲存Pattern中每個字元最後一次出現的位置,從左至右的順序
     */
    private void initMap() {
        for (int i = 0; i < pattern.length(); i++) {
            this.map.put(pattern.charAt(i), i);

        }
    }

    /**
     * 普通的字串遞迴匹配,匹配失敗就前進一位
     */
    public List<Integer> normalMatch() {
        //匹配失敗,繼續往下走
        if (!matchFromSpecialPos(currentPos)) {
            currentPos += 1;

            if ((text.length() - currentPos) < pattern.length()) {
                return matchedPosList;
            }
            normalMatch();
        } else {
            //匹配成功,記錄位置
            matchedPosList.add(currentPos);
            currentPos += 1;
            normalMatch();
        }

        return matchedPosList;
    }

    /**
     * Sunday匹配,假定Text中的K字元的位置為:當前位移量+Pattern字串長度+1
     */
    public List<Integer> sundayMatch() {
        // 如果沒有匹配成功
        if (!matchFromSpecialPos(currentPos)) {
            // 如果Text中K字元沒有在Pattern字串中出現,則跳過整個Pattern字串長度
            if ((currentPos + pattern.length() + 1) < text.length()
                    && !map.containsKey(text.charAt(currentPos + pattern.length() + 1))) {
                currentPos += pattern.length();
            }else {
                // 如果Text中K字元在Pattern字串中出現,則將Text中K字元的位置和Pattern字串中的最後一次出現K字元的位置對齊
                if ((currentPos + pattern.length() + 1) > text.length()) {
                    currentPos += 1;
                } else {
                    currentPos += pattern.length() - (Integer) map.get(text.charAt(currentPos + pattern.length()));
                }
            }

            // 匹配完成,返回全部匹配成功的初始位移
            if ((text.length() - currentPos) < pattern.length()) {
                return matchedPosList;
            }

            sundayMatch();
        }else {
            // 匹配成功前進一位然後再次匹配
            matchedPosList.add(currentPos);
            currentPos += 1;
            sundayMatch();
        }
        return matchedPosList;
    }

    /**
     * 檢查從Text的指定位移量開始的子串是否和Pattern匹配
     */
    public boolean matchFromSpecialPos(int pos) {
        if ((text.length()-pos) < pattern.length()) {
            return false;
        }

        for (int i = 0; i < pattern.length(); i++) {
            if (text.charAt(pos + i) == pattern.charAt(i)) {
                if (i == (pattern.length()-1)) {
                    return true;
                }
                continue;
            } else {
                break;
            }
        }

        return false;
    }

    public static void main(String[] args) {
        SundySearch sundySearch = new SundySearch("hello 啊啊 阿道夫 adfsadfklf adf234masdfsdfdsfdsfdsffwerwrewrerwerwersdf2666sdflsdfk", "adf");

        long begin = System.nanoTime();
        System.out.println("NormalMatch:" + sundySearch.normalMatch());
        System.out.println("NormalMatch:" + (System.nanoTime() - begin));

        begin = System.nanoTime();
        System.out.println("SundayMatch:" + sundySearch.sundayMatch());
        System.out.println("SundayMatch:" + (System.nanoTime() - begin));

    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.