文本比较算法三——SUNDAY 算法

 

SUNDAY 算法描述:

字符串查找算法中,最著名的两个是KMP算法(Knuth-Morris-Pratt)和BM算法(Boyer-Moore)。两个算法在最坏情况下均具有线性的查找时间。但是在实用上,KMP算法并不比最简单的c库函数strstr()快多少,而BM算法则往往比KMP算法快上3-5倍。但是BM算法还不是最快的算法,这里介绍一种比BM算法更快一些的查找算法。

例如我们要在"substring searching algorithm"查找"search",刚开始时,把子串与文本左边对齐:

substring searching algorithm
search
^

结果在第二个字符处发现不匹配,于是要把子串往后移动。但是该移动多少呢?这就是各种算法各显神通的地方了,最简单的做法是移动一个字符位置;KMP是利用已经匹配部分的信息来移动;BM算法是做反向比较,并根据已经匹配的部分来确定移动量。这里要介绍的方法是看紧跟在当前子串之后的那个字符(上图中的 'i')。

显然,不管移动多少,这个字符是肯定要参加下一步的比较的,也就是说,如果下一步匹配到了,这个字符必须在子串内。所以,可以移动子串,使子串中的最右边的这个字符与它对齐。现在子串'search'中并不存在'i',则说明可以直接跳过一大片,从'i'之后的那个字符开始作下一步的比较,如下图:

substring searching algorithm
     search
     ^

比较的结果,第一个字符就不匹配,再看子串后面的那个字符,是'r',它在子串中出现在倒数第三位,于是把子串向前移动三位,使两个'r'对齐,如下:

substring searching algorithm
        search
       ^

哈!这次匹配成功了!回顾整个过程,我们只移动了两次子串就找到了匹配位置,是不是很神啊?!可以证明,用这个算法,每一步的移动量都比BM算法要大,所以肯定比BM算法更快。

下面是这个算法的c代码。注意我假设了每个字符的值都介于0-127之间(即纯ascii码)。

char *qsearch(const char *text, int n, const char *patt, int m)
{
    // get the length of the text and the pattern, if necessary
    if (n < 0)
        n = strlen(text);
    if (m < 0)
        m = strlen(patt);
    if (m == 0)
        return (char*)text;

    // construct delta shift table
    int td[128];
    for (int c = 0; c < 128; c++)
        td[c] = m + 1;

    const char* p;
    for (p=patt; *p; p++)
        td[*p] = m - (p - patt);

    // start searching...
    const char *t, *tx = text;

    // the main searching loop
    while (tx + m <= text + n) {
        for (p = patt, t = tx; *p; ++p, ++t) {
            if (*p != *t)  // found a mismatch
                break;
        }
        if (*p == 0)   // Yes! we found it!
            return (char*)tx;
        tx += td[tx[m]];  // move the pattern by a distance
    }

    return NULL;
}

 

注:这个查找算法称为Sunday算法,它是BM算法的一种改进型。

 

 

 

原文地址:https://www.cnblogs.com/huhu0013/p/3227811.html