字符串匹配查找算法中,最著名的兩個是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));
}
}