Quick Search algorithm (string)

本文深入探讨了QuickSearch算法,它简化了Boyer-Moore算法,仅使用坏字符移位表进行文本搜索。通过优化预处理阶段和搜索阶段,该算法在短模式和大型字母表中表现出极高的效率。

Main features:

  • simplification of the Boyer-Moore algorithm;
  • uses only the bad-character shift;
  • easy to implement;
  • preprocessing phase in O(m+sigma) time and O(sigma) space complexity;
  • searching phase in O(mn) time complexity;
  • very fast in practice for short patterns and large alphabets.

Description:

The Quick Search algorithm uses only the bad-character shift table (see chapter Boyer-Moore algorithm). After an attempt where the window is positioned on the text factor y[j .. j+m-1], the length of the shift is at least equal to one. So, the character y[j+m] is necessarily involved in the next attempt, and thus can be used for the bad-character shift of the current attempt.

The bad-character shift of the present algorithm is slightly modified to take into account the last character of x as follows: for c in Sigma, qsBc[c]=min{i : 0  < i leq m and x[m-i]=c} if c occurs in x, m+1 otherwise (thanks to Darko Brljak).

The preprocessing phase is in O(m+sigma) time and O(sigma) space complexity.

During the searching phase the comparisons between pattern and text characters during each attempt can be done in any order. The searching phase has a quadratic worst case time complexity but it has a good practical behaviour.

The C Code:

void preQsBc(char *x, int m, int qsBc[]) {

   int i;

   for (i = 0; i < ASIZE; ++i)

      qsBc[i] = m + 1;   for (i = 0; i < m; ++i)

      qsBc[x[i]] = m - i;

}

void QS(char *x, int m, char *y, int n) {

   int j, qsBc[ASIZE];

   /* Preprocessing */

   preQsBc(x, m, qsBc);

    /* Searching */

   j = 0;

   while (j <= n - m) {

      if (memcmp(x, y + j, m) == 0)

         OUTPUT(j);

      j += qsBc[y[j + m]];

               /* shift */

   }

}

 

Reference

http://www-igm.univ-mlv.fr/~lecroq/string/node19.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值