您的位置:首页 > 其它

时空权衡——字符串匹配(Time/Space Tradeoff - Horspool's String Matching)

2017-06-10 09:24 691 查看

时空权衡——字符串匹配(Time/Space Tradeoff - Horspool’s String Matching)

字符串匹配简介(Introduction)

String matching is to find a place where one or several strings(called pattern) are found within a large string or text. For example,

Text: STRINGSEARCH EXAM P

Pattern: EXAM

算法(Algorithm)

Before the actual matching process starts, we do some pre-processes of strings, which is also called input enhancement. Build a shift table to help matching.

For example,



Compare each character in the pattern from right to left. Shift the pattern “EXAM” 4 position to right since I is not in the pattern.



Shift 3 position because E in the text occurs in the first place in the pattern.



Shift 4 position cause C in the text is not in the pattern.



Shift 1 position because A in the text occurs in the third place in the pattern.



Here, we get match.

How many position to shift after comparison? - The answer is shift table.



Constructing Shift Table Pseudocode

function FindShifts(P[0..m-1])
for i ⟵ 0 to a do
Shift[i] ⟵ m
for j ⟵ 0 to m-1 do
Shift[P[j]] ⟵ m-j-1


Horspool’s String Matching Algorithm Pseudocode

function Horspool(P[0..m-1], T[0..n-1])
FindShifts(P)
i ⟵ m-1
while i < n do
k ⟵ 0
while k < m and P[m-1-k] = T[i-k] do
k  ⟵ k + 1
if k = m then
return i - m + 1
else
i  ⟵ i + Shift[T[i]]
return -1


Ja
972c
va Code


I will update ASAP.


写在后面的话(PS)

There are three algorithms for string matching: Brute force algorithm, Horspool’s algorithm and Rabin-Karp algorithm. Understand each of them.

Welcome questions always and forever.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: