字串搜尋演算法

字串搜尋演算法String searching algorithms)又稱字串比對演算法string matching algorithms)是一种搜索算法,是字串演算法中的一類,用以試圖在一長字符串或文章中,找出其是否包含某一個或多個字符串,以及其位置。

最直觀的解法是比對,如下例中,在字符串haystack中找出字符串needle

char* haystack;
char* needle;
int hlen, nlen, found;
int i,j,k;

found = 0;
hlen = strlen(haystack);
nlen = strlen(needle);
for (i = 0; i
上例中,若字符串needle存在於字符串haystack中,則傳回1,否則傳回0。

但是此直觀算法的複雜度為 O(mn),其中haystack的長度為n、needle的長度為m,所以另有更快速的算法。

部分算法比较
m 为模式的长度, n 为要搜索的字符串长度, k为字母表长度。

外部連結
*[http://www.cs.ucr.edu/%7Estelo/pattern.html Huge (maintained) list of pattern matching links]
*[http://johannburkard.de/software/stringsearch/ StringSearch — high-performance pattern matching algorithms in Java] – Implementations of many String-Matching-Algorithms in Java (BNDM, Boyer-Moore-Horspool, Boyer-Moore-Horspool-Raita, Shift-Or)
*[http://www-igm.univ-mlv.fr/~lecroq/string/index.html Exact String Matching Algorithms—Animation in Java]
*[https://web.archive.org/web/20070718051302/http://www.dcs.shef.ac.uk/~sam/stringmetrics.html String similarity metrics]
*Project Dedupe http://dedupe.sourceforge.net
*[https://web.archive.org/web/20070630034621/http://www.concentric.net/~Ttwang/tech/stringscan.htm Boyer-Moore-Raita-Thomas]

评论 (0)

  • 还没有评论,来抢沙发吧。