2011-05-24 72 views
3

我需要找到一個子串字符串模擬一個巨大的字符串。源巨大的字符串可能長達100 Mb。模式很短(10-100個字符)。問題是我需要找到不僅僅是確切的子字符串,而且還需要找出與幾個字符中的模式不同的類似子字符串(允許的最大錯誤數作爲參數)。類似的子字符串快速搜索

有什麼想法如何加快算法?

+0

您是否在尋找一種針對單個查詢進行優化的算法?或者是[索引策略](http://en.wikipedia.org/wiki/Index_(search_engine)),它將創建100MB源文本的數據結構,以便優化所有類似性質的查詢。 – rwong 2011-06-19 11:29:07

回答

1

1)有很多與字符串搜索有關的算法。其中之一是着名的Knuth–Morris–Pratt Algorithm

2)您可能還想檢查正則表達式(「正則表達式」),無論您使用何種語言。他們一定會幫助您找到與原始字符串「類似」的子字符串。

即【JAVA]

String pat = "Home"; 
String source = "IgotanewHwme"; 

for(int i = 0; i < pat.length(); i++){ 
    //split around i .. not including char i itself .. instead, replace it with [a-zA-Z] and match using this new pattern. 
    String new_pat = "("+pat.substring(0, i)+")"+ "[a-zA-Z]" + "("+pat.substring(i+1, pat.length())+")"; 
    System.out.println(new_pat); 
    System.out.println(source.matches("[a-zA-Z]*"+new_pat+"[a-zA-Z]*")); 
} 

,我認爲這是容易使其接受任何數目的錯誤計數。