时空权衡——字符串匹配(Time/Space Tradeoff

来源:互联网 发布:网络流行尴舞视频 编辑:程序博客网 时间:2024/05/17 07:51

时空权衡——字符串匹配(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            ii + Shift[T[i]]    return -1

Java 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.