时空权衡——字符串匹配(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.
相关文章推荐
- 时空权衡——哈希法(Time/Space Tradeoff - Hashing)
- 时空权衡——斐波那契数列(Time/Space Tradeoff - Fibonacci Sequence)
- Space and time trade-off in n queen problem
- [时空权衡]字符串匹配KMP算法代码(引自算法导论)
- [时空权衡]字符串匹配算法 KMP
- 时空权衡之字符串匹配中的输入增强技术
- 时空权衡:利用额外的空间提高字符串匹配的速度
- 软件的艺术之美源于权衡(Trade-off)
- 算法-字符串匹配(String Matching)-(2)-有限自动机
- 算法-字符串匹配(String Matching)-(1)-朴素匹配和Rabin-Karp算法
- [转]软件的艺术之美源于权衡(Trade-off)
- 字符串匹配(string matching)算法之一 (Naive and Rabin_Karp)
- kafka:权衡(trade-off)
- 字符串匹配算法 之 (Horspool )Boyer-Moore-Horspool
- 机器学习算法中的偏差-方差权衡(Bias-Variance Tradeoff)
- 方差与偏差的权衡(trade-off)
- 时空权衡在模式匹配算法中的应用(JAVA)--Horspool算法(简化版BM算法)
- Bias-Variance Tradeoff(权衡偏差与方差)
- 软件的艺术之美源于权衡(Trade-off)
- 字符串匹配算法horspool