Horspool algorithm

Idea

http://www.iti.fh-flensburg.de/lang/algorithmen/pattern/horsen.htm

The Boyer-Moore algorithm uses two heuristics in order to determine the shift distance of the pattern in case of a mismatch: the bad-character and the good-suffix heuristics. Since the good-suffix heuristics is rather complicated to implement there is a need for a simple algorithm that is based merely on the bad-character heuristics. Due to an idea of Horspool [Hor 80], instead of the "bad character" that caused the mismatch, in each case the rightmost character of the current text window is used for determining the shift distance.

Example:  

0 1 2 3 4 5 6 7 8 9 ...
abcabdaacba
bcaab      
 bcaab     
 
0 1 2 3 4 5 6 7 8 9 ...
abcabdaacba
bcaab      
    bcaab  
   
(a)   Boyer-Moore (b)   Horspool
   

In this example, t0, ..., t4  =  a b c a b is the current text window that is compared with the pattern. Its suffix a b has matched, but the comparison c-a causes a mismatch. The bad-character heuristics of the Boyer-Moore algorithm (a) uses the "bad" text character c to determine the shift distance. The Horspool algorithm (b) uses the rightmost character b of the current text window. The pattern can be shifted until the rightmost occurrence of b in the pattern matches the text character b, where the occurence at the last position of the pattern does not count.

Like the Boyer-Moore algorithm, the Horspool algorithm assumes its best case if every time in the first comparison a text symbol is found that does not occur at all in the pattern. Then the algorithm performs just O(n/m) comparisons.

 

Preprocessing

The function occ required for the bad-character heuristics is computed slightly different as in the Boyer-Moore algorithm. For every alphabet symbol a, the function value occ(pa) is equal to the rightmost position of a in p0 ... pm-2, or -1, if a does not occur at all. Observe that the last symbol pm-1 of the pattern is not taken into account.

Example:  

  • occ(text, x) = 2
  • occ(text, t) = 0
  • occ(next, t) = -1

The occurrence function for a certain pattern p is stored in an array occ that is indexed by the alphabet symbols. For every symbol a element A the entry occ[a] holds the corresponding function value occ(pa).

Given a pattern p, the following function horspoolInitocc computes the occurrence function.

void horspoolInitocc()
{
    int j;
    char a;

    for (a=0; a<alphabetsize; a++)
        occ[a]=-1;

    for (j=0; j<m-1; j++)
    {
        a=p[j];
        occ[a]=j;
    }
}



Searching algorithm
 

As in the Boyer-Moore algorithm, the pattern is compared from right to left with the text. After a complete match or in case of a mismatch, the pattern is shifted according to the precomputed function occ.

void horspoolSearch()
{
    int i=0, j;
    while (i<=n-m)
    {
        j=m-1;
        while (j>=0 && p[j]==t[i+j]) j--;
        if (j<0) report(i);
        i+=m-1;
        i-=occ[t[i]];
    }
}



References
 

   
[Hor 80] R.N. Horspool: Practical Fast Searching in Strings. Software - Practice and Experience 10, 501-506 (1980)
  
[1]http://www-igm.univ-mlv.fr/~lecroq/string/  
[2]http://www.inf.fh-flensburg.de/lang/algorithmen/pattern/stringmatchingclasses/HorspoolStringMatcher.java   Horspool algorithm as a Java class source file
### 回答1: Horspool算法是一种字符串匹配算法,用于在一个文本串中查找一个模式串的出现位置。它是由Robert S. Horspool在198年提出的,是Boyer-Moore算法的一种改进。 该算法的基本思想是:从右往左匹配模式串和文本串,如果匹配失败,则根据模式串中下一个字符在模式串中的位置,将模式串向右移动一定的距离,然后再次从右往左匹配。这个距离称为“坏字符距离”,可以通过预处理模式串中每个字符最后一次出现的位置来计算。 Horspool算法的时间复杂度为O(n),其中n为文本串的长度。它比Brute-Force算法和Naive算法要快,但比Boyer-Moore算法和Knuth-Morris-Pratt算法要慢。 ### 回答2: Horspool算法是一种字符串匹配算法,用于在给定文本中查找给定模式的出现,并返回它的位置。它是一种线性时间算法,因为在最坏情况下它只需O(n)时间来完成,其中n是文本字符串的长度。 Horspool算法的核心思想是从右到左跳过一些字符,这样可以使算法更快地处理字符串。它使用一个预处理表来确定如何跳过字符。该表名为“跳表”(Shift Table),其中每个字符的值表示当出现在模式中时,模式可以“跳过”文本中的字符的数量。这意味着如果模式中最后一个字符的跳转表中的值为k,则算法将在文本中向右移动k个位置(跳过k个字符)进行比较。 Horspool算法的具体步骤如下: 1. 创建跳转表,根据模式字符串中每个字符的位置为它们分配跳转值。 2. 从文本的起始位置开始,将模式字符串与文本字符串中的对应字符逐个进行比较。 3. 如果相匹配,则继续匹配下一个字符。如果不匹配,则根据跳转表中模式字符串的最后一个字符的值,跳过一些字符。如果跳到字符串结尾,则说明匹配失败。 4. 重复步骤2和3,直到匹配到模式字符串为止,或者检索到文本的末尾并且仍未找到匹配项为止。 Horspool算法对于简单的情况效果非常好,但对于某些模式或某些文本会导致算法的性能下降。在这种情况下,其他更高效的算法如Boyer-Moore算法或Knuth-Morris-Pratt算法可能更具优势。 ### 回答3: Horspool算法是一种字符串匹配算法,它可以在时间复杂度O(n)的情况下查找一个模式串在文本串中的第一次出现位置。Horspool算法基于一种简单的思想,即当一个字符在模式串中出现时,我们可以快速地判断它是否在文本串中出现。 Horspool算法的核心是坏字符规则,它将文本串中当前匹配位置的字符x同模式串中最后一次出现x的位置进行比较,如果不匹配,则将模式串向右移动x在模式串中的距离。如果x不在模式串中,则将模式串整体右移一位。这样,在匹配时,算法只需要考虑文本串中的坏字符位置即可。 此外,为了加速匹配过程,Horspool算法还采用了“好后缀”规则。这个规则是指,当模式串的某个后缀与文本串中的某个子串匹配时,能够尽可能地保留匹配的结果。具体实现方法是,当文本串中某个位置与模式串中的某个位置不匹配时,算法将模式串向右移动i-j的距离,其中i是文本串中当前位置,j是模式串中好后缀的结尾位置。 综上所述,Horspool算法是一种高效的字符串匹配算法,其设计思想简单,实现也不难。它在实际应用中被广泛使用,在一些大规模的字符串匹配问题中,Horspool算法的速度能够超过其他常用的字符串匹配算法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值