查詢字串之 BF演算法

2021-06-23 01:31:27 字數 558 閱讀 9201

bf 演算法最簡單的查詢查詢字串演算法:

匹配串s同模式串p,逐個位元組比較,失敗後向後移動乙個位元組繼續比較。直至成功或者比較結束。

形如  :--a a a a ----

比較1:--b b b b

比較2:--    b b b b ---

比較3:          b b b b --

這種方式簡單粗暴如其名(brute force),但是效率較低,用在比較短的字串查詢,沒有問題。

時間複雜度o(strlen(s)*strlen(p))

空間複雜度o(1)

以下**山寨自vc strstr:

const char* strstr( const char* s , const char* p )

if ( !*p )

/*if ( strlen(p) > strlen(s) )

*/char *s = (char*)s;

char *s1,*s2;

while( *s )

return null;

}

字串查詢 BF演算法

字串查詢 bf演算法 include include int strfind char str,char partner else if j plen return pos int main 字串查詢 最普通的演算法 串 a 為 abcadef 而串 b 為 cad 最理想的時間複雜度 o n n ...

查詢字串之boyer moore演算法

給出字串p和t,長度分別為n和m。找出p在t中出現的所有位置。int index char p,char t,int pos else if j strlen p return i strlen p 1 else return 1 suffix tree演算法需要對字串t進行預處理,而boyer m...

查詢字串之 KMP演算法

bf演算法中 匹配串 a a a a b c c 模式串 a a a a a b c 後移後 a a a a a b c 在位置4匹配失敗,按照bf演算法,下一輪匹配開始,匹配串指標只會向後後偏移一位,kmp 演算法是對bf演算法的改進,通過預處理,來改進每次向後的偏移量.kmp演算法實在有點繞。看...